Error when calling C # WebResponse GetResponse () method: The underlying connection was closed: Connection closed unexpectedly.

Today, when get a login method, because the request is to test the server, to access the login interface methods, but the request process, 

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

 WebResponse response = request.GetResponse();;

Request response given: underlying connection was closed: the connection is closed unexpectedly. Method login request with a postman given as follows:

From the word meaning interface method table method does not allow the connection open. postman can not request. Refer https://blog.csdn.net/jiangyu1013/article/details/82120052

If the postman or the request can not, look for the following reasons:

To address this issue, there are several possibilities:

1: When requested parameter limits added, categories such as:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Timeout = 120 * 1000;
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.ContentType = contentType;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertificateValidation);

2 :( My question is for this reason) when accessing the interface, not the local host file configuration information, refer to https://jingyan.baidu.com/article/fedf073754695735ac89772c.html

Access path C: \ Windows \ System32 \ drivers \ etc [To open the hosts file to open the administrator, or hungry can save content changes], to increase the interface configuration information for your visit in the last line, categories such as:

192.168.0.XXX api.XXXXX.com is your interface to access URL

# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
192.168.0.XXX   api.XXXXX.com

Save it. Run the project again, and finally found a surprise visit success! !

 

 

Because sometimes need to access the project in the local area network, introduced here how to access the project in the local area network VS: [not interested in such as this, in order to save your precious time and can be ignored Oh ~]

1. Open .vs directory under the project directory, modify the contents of the file applicationhost.config

2:【192.168.0.110是你的电脑的IP,可通过cmd ipconfig查看本机IP】

          <!--找到项目名-->

         < site  name="muTian.sysAdmin.UI.Portal" id="2">
             < application  path="/" applicationPool="Clr4IntegratedAppPool">
                 < virtualDirectory  path="/" physicalPath="D:\CsharpProject\muTian.sysAdmin\muTian.sysAdmin.UI.Portal" />
             </ application >
             < bindings >
                     <!--这一行是默认就有的-->
                 < binding  protocol="http" bindingInformation="*:10829:localhost" />
                 <!--这一行是增加的-->
                     <binding protocol="http" bindingInformation="*:10829:192.168.0.110" />
             </ bindings >
         </ site >      

3: Open cmd [Note] to open the Administrator [10829 is the port number of your project, right inside the VS - >> property to view]

执行 netsh http add urlacl url=http://192.168.0.110:59961/ user=everyone

In this context command:
Show cacheparam - display cache parameters HTTP services.
show cachestate - listed cached URI resources and their associated properties.
show iplisten - displays all IP addresses IP listen list.
show servicestate - displays snapshot HTTP service.
show setting - displays the set value services.
show sslcert - display the SSL certificate binding.
show timeout - a timeout value display services.
show urlacl - display URL namespaces reserved items.

Recompiled to run the project, remember to turn on as administrator provinces VS, otherwise an error status code 503 will appear

So far, on request Questions introduced complete, if the problem can be Comments! !

 

Guess you like

Origin www.cnblogs.com/fengyulv/p/11824369.html