2

It works fine to browse this page : http://www.litteraturmagazinet.se/arga-bibliotekstanten/boklogg/favorit-i-repris-9560835 in a regular browser(for example Chrome).

But when I use the following code to fetch the website I get Internal Server Error (500)?

This is the code I use (and it works great on all other webpages I have tried) :

HttpWebRequest request;
WebResponse webresponse;
request = (HttpWebRequest)HttpWebRequest.Create(url);
webresponse = (HttpWebResponse)request.GetResponse();

The exception is thrown in GetResponse.

I have found for example this : HttpWebRequest.GetResponse() returns error 500 Internal Server Error, but I do not understand it? Why does not request.GetResponse work with this specific webpage? And How do I know what to put in the header(It would be great if it dident hade to be updated later on to diffrent versions)?

Community
  • 1
  • 1
Banshee
  • 15,376
  • 38
  • 128
  • 219
  • What exception is `GetResponse()` throwing? – 48klocs Nov 27 '13 at 17:33
  • 500 is as good as any other response. If you think server should be returning something different - figure out what it expect and construct request appropriately. One way is to use Fiddler and make sure you request is exactly the same as one sent by a browser. – Alexei Levenkov Nov 27 '13 at 17:34
  • Side note: Are you really getting that exception from `Create` call? That is very strange as `Create` should not be making any outgoing calls... – Alexei Levenkov Nov 27 '13 at 17:35

1 Answers1

10

I tried your url with wfetch with no headers and i get a 500 as well.

you have to set a proper user-agent in the headers of your request.

HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
myHttpWebRequest.UserAgent=".NET Framework Test Client";
dolomitt
  • 261
  • 3
  • 5
  • Seems like setting any agent fixes it, i used: UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; – WtFudgE Dec 24 '20 at 05:58