ykh
ykh

Reputation: 1835

HttpWebRequest.GetResponseStream returns an empty string

I have been trying every way possible to download get the response stream from a page using HttpWebRequest, which works fine on everything else but no this page. As you see the data returned is an HTML but it have a some meta tag which I am not expert in, but what ever i try to get this block

{"error":4,"message":"Unsupported link format or unsupported hoster"}

It seems that I can't, I tried to specify the GET content-type as "text/json" but nothing helped.

Below is the HTML content returned when I open the page on the browser, but in code it returns empty string.

    <html>
    <meta style="visibility: hidden !important; display: block !important; width: 0px !important; height: 0px !important; border-style: none !important;"></meta>
    <head></head>
    <body>
      <pre style="word-wrap: break-word; white-space: pre-wrap;">{"error":4,"message":"Unsupported link format or unsupported hoster"}
      </pre>
    </body>
    </html>

Edit:

I have tried copy the same html above in a page on localhost and tried to fetch it's content and it actually worked, could there maybe some restriction in the IIS that could prevent fetching content ?

Upvotes: 1

Views: 5050

Answers (3)

Ramon Smits
Ramon Smits

Reputation: 2618

I had the same problem and the cause was that I previously had set the method to HEAD and in later revisions had the need to parse the body.

Upvotes: 0

usr
usr

Reputation: 171246

Do you have evidence that the problem is with the client? The more appropriate question is: Why is the server sending this strange content? The client just receives whatever the server sends.

I suggest you debug the server to find out, or ask a new question containing your server-side code and asking specifically about it.

Upvotes: 1

Ionică Bizău
Ionică Bizău

Reputation: 113485

Try to use the WebRequest class. Here you find the documentation for it.

You have this example in the page:

// Create a request for the URL.        
WebRequest request = WebRequest.Create ("[your_url]");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
// Display the status.
Console.WriteLine (response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content. 
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();

This will print in Console the HTML content of that page.

Upvotes: 0

Related Questions