HttpWebResponse remote server returned error: (500) Resolution of internal server error

Source: Internet
Author: User

In the work with C # developed a small program, and constantly access to request a site page, in the loop sometimes reported "remote server return Error: (500) Internal server error", sometimes not, the timing is not the same. began to think that the site is a problem, and then the site can be normal access, and that is the problem of their own programs.

 for (int i = refreshaccount.startnum; I <= Refreshaccount.endnum; I++) { String data2 = urlstr ;  loadbranch2request = (HttpWebRequest) Httpwebrequest.create (DATA2); Loadbranch2request.cookiecontainer = Request2. Cookiecontainer;  loadbranch2response = (HttpWebResponse) Loadbranch2request.getresponse (); reader = new StreamReader ( Loadbranch2response.getresponsestream (), Encoding.UTF8); string result3 = reader. ReadToEnd (); Loadbranch2response.close (); Reader. Close (); ............ }               

From two directions to find the reason, one is the correct wording of HttpWebRequest, the second is the cause of the above problems. After consulting the solution of the same kind of problem, it is generally judged as the problem of request message.

Two reference posts are as follows:

C # Analog HTTP sends a post or GET request: https://www.cnblogs.com/aaronguo/p/7063790.html

HttpWebResponse remote server returned error: (500) Internal server error Resolution: 5330323

Take it for granted that you use the Get method without setting the value of the loadbranch2request.contenttype, and actually setting the value of method and ContentType solves the problem above.

Loadbranch2request.method = "GET";

Loadbranch2request.contenttype = "Text/html;charset=utf-8";

 for(inti = Refreshaccount.startnum; I <= refreshaccount.endnum; i++) {String data2=Urlstr; Loadbranch2request=(HttpWebRequest) httpwebrequest.create (DATA2); Loadbranch2request.cookiecontainer=Request2. Cookiecontainer; Loadbranch2request.method = "GET"; Loadbranch2request.contenttype = "Text/html;charset=utf-8"; Loadbranch2response=(HttpWebResponse) loadbranch2request.getresponse (); Reader=NewStreamReader (Loadbranch2response.getresponsestream (), Encoding.UTF8);stringRESULT3 =Reader.  ReadToEnd ();  Loadbranch2response.close (); Reader.   Close (); ............ }

If you want to get the error page source code, you can use the following methods

Reference: https://www.cnblogs.com/cresuccess/archive/2009/12/09/1619977.html

You can get the source code for the server segment page with the following code, regardless of whether the error occurred or not.

HttpWebResponse Res;
Try
{
res = (HttpWebResponse) req. GetResponse ();
}
Catch (WebException ex)
{
res = (HttpWebResponse) ex. Response;
}
New StreamReader (Res. GetResponseStream (), Strencode);
strHTML = Sr. ReadToEnd ();
When an exception occurs, not only does the StatusCode mark the HTTP error code in WebException, but its Response property also contains the WebResponse sent by the server to indicate the actual HTTP error encountered.

HttpWebResponse remote server returned error: (500) Resolution of internal server error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.