C ා using HttpWebRequest class to send post request, there is a problem that "socket (Protocol / network address / port) can only be used once"

Statement: Although the problem has been solved, the specific principle has not been understood. Welcome to add.

 

Recently, a problem occurred in the website. In C ා, use the HttpWebRequest class to send post requests. Occasionally, there will be an exception that "socket (Protocol / network address / port) can only be used once". Obviously, the port is occupied.

Cause investigation:

1. On the Internet, it is the port occupied by other programs at most: because it is online and can run normally sometimes, the possibility of other programs occupying the port is ruled out. Moreover, port 80 is used by my website.  

2. The second possible reason is the poor performance of the interface, which takes up a long processing time: I add time logs to the target interface of post (because the interface is also provided by the website itself, so it can be monitored). It is found that the processing time is about 30ms-50ms, and the website traffic is not very large, so the processing speed is very fast, which should not be reasonable The port is occupied.

 

Let's take a look at the post code, as follows:

private string sendHttpWebRequest(bool isPost, string sendData, string requestURL)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] data = encoding.GetBytes(sendData);
        // Preparation web request
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestURL);
        if (isPost)
        {
            myRequest.Method = "POST";
        }
        else
        {
            myRequest.Method = "GET";
        }
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.ContentLength = data.Length;
        if (isPost)
        {
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
        }
    }

 

Finally, I found that the above code did not receive the return because of the design problem of the interface itself. The interface is only for notification and does not care about the return results (which is not a good design in itself, of course).

But after the test, the problem is that there is no receiving return. After receiving the return code, the exception "socket (Protocol / network address / port) is only allowed to be used once" disappears!

Finally, the post code is as follows:

private string sendHttpWebRequest(bool isPost, string sendData, string requestURL)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] data = encoding.GetBytes(sendData);
        // Preparation web request
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestURL);
        if (isPost)
        {
            myRequest.Method = "POST";
        }
        else
        {
            myRequest.Method = "GET";
        }
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.ContentLength = data.Length;
        if (isPost)
        {
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
        }
   //Get response
  HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
   return
reader.ReadToEnd();
}

At this point, "socket (Protocol / network address / port) can only be used once" is no longer a problem...  

Tags: C# encoding socket network

Posted on Mon, 02 Dec 2019 09:47:13 -0800 by ryans18