Solution to the strange problem of HttpWebRequest on the WEB

Source: Internet
Author: User
Tags servervariables

Today, I made a small program for a client in the LAN to obtain the public IP address of the LAN. The method is to log on to the Internet and let the Internet tell you what the public IP address is in the LAN. The method is as follows:

Copy codeThe Code is as follows:
Uri uri = new Uri ("http://www.jb51.net /");
HttpWebRequest req = (HttpWebRequest) WebRequest. Create (uri );
Req. Method = "POST ";
Req. ContentType = "application/x-www-form-urlencoded ";
Req. ContentLength = 0;
Req. CookieContainer = new CookieContainer ();
Req. GetRequestStream (). Write (new byte [0], 0, 0 );
HttpWebResponse res = (HttpWebResponse) (req. GetResponse ());
StreamReader rs = new StreamReader (res. GetResponseStream (), System. Text. Encoding. GetEncoding ("GB18030 "));
String s = rs. ReadToEnd ();
Rs. Close ();
Req. Abort ();
Res. Close ();
System. Text. RegularExpressions. Match m = System. Text. RegularExpressions. Regex. Match (s, @ "ClientIP :\[(? <IP> [0-9 \.] *) \] ");
If (m. Success) IP = m. Groups ["IP"]. Value;


This program passed the test on the application, but on the WEB, it is not dead, and an error is reported: Unable to connect to fail, cannot reach the remote server.
How can this problem occur when the remote server cannot be connected? I can access the Internet clearly, and the application shows that my code is correct. Is it a port problem? But I am localhost, And it is useless to change the local virtual site port. Then we started to get crazy About baidu and google, and found many similar problems that were not solved.
No way, you have to find the difference between the application and the WEB. In fact, the truth is always passed by yourself. When the application calls this code, it will automatically call the Internet Settings of IE, if you access the Internet through a proxy locally, it will connect to the remote server through the proxy. However, the WEB is different from the application. Note the iesettings shown in the figure below:

All the WEB users know that we use localhost for Local WEB debugging. If we use a proxy, the debugging will fail, therefore, the "Do not use proxy server for local addresses" at the bottom will be checked.
Now I can understand that it turns out to be a ghost. I am debugging locally, but I cannot access the Internet without a proxy. Add the following two sentences to the Code:

IWebProxy iWebProxy = new WebProxy ("172.17.85.16: 8080", true );
Req. Proxy = iWebProxy;

This solution solves the problem, but it only solves the problem. Even if it is solved, this solution is not feasible.
In fact, as a server, there is no need to go around a large circle. We also use other sites to obtain our own IP addresses locally. Our server knows when sending a local request, as long as a simple sentence: Page. request. userHostAddress, I will not complain here, just say a method to get the real IP address of the client:

String IP = (Request. ServerVariables ["HTTP_VIA"]! = Null)
? Request. ServerVariables ["HTTP_X_FORWARDED_FOR"]. ToString ()
: Request. ServerVariables ["REMOTE_ADDR"]. ToString ();

Related Article

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.