3

I'm using HttpWebRequest to make a request to a url:

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(urlAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

but it throws error 500 (Internal Server Error) but when i visit the URLAddress with browser it works fine, urlAddress= www.khademnews.com

it is a simple GET operation but it throws an exception for me how can I solve this?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Ehsan
  • 1,662
  • 6
  • 28
  • 49
  • 2
    The server apparently expects some HTTP headers in the request that a web browser typically sends but the HttpWebRequest does not. You need to figure out which headers these are (for example, using [Fiddler](http://www.fiddler2.com/fiddler2/)) and add them to the HttpWebRequest. – dtb Jun 29 '11 at 06:40

1 Answers1

12

You might need to set up the user agent as some sites might require it. Also you could use a WebClient to simplify your code:

using (var client = new WebClient())
{
    client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0";
    string result = client.DownloadString("http://www.khademnews.com");
}

The server might expect other headers as well. You could check with FireBug which headers are sent went you perform the request in your browser and add those headers.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928