GetRequestStream() is throwing time out exception when posting data to HTTPS url

前端 未结 5 1708
悲&欢浪女
悲&欢浪女 2021-02-07 06:34

I\'m calling an API hosted on Apache server to post data. I\'m using HttpWebRequest to perform POST in C#.

API has both normal HTTP and secure layer (HTTPS) PORT on the

5条回答
  •  借酒劲吻你
    2021-02-07 07:15

    I ran into this, too. I wanted to simulate hundreds of users with a Console app. When simulating only one user, everything was fine. But with more users came the Timeout exception all the time.

    Timeout occurs because by default the ConnectionLimit=2 to a ServicePoint (aka website). Very good article to read: https://venkateshnarayanan.wordpress.com/2013/04/17/httpwebrequest-reuse-of-tcp-connections/

    What you can do is:

    1) make more ConnectionGroups within a servicePoint, because ConnectionLimit is per ConnectionGroups.

    2) or you just simply increase the connection limit.

    See my solution:

    private HttpWebRequest CreateHttpWebRequest(string userSessionID, string method, string fullUrl, U uploadData)
    {
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(fullUrl);
        req.Method = method; // GET PUT POST DELETE
        req.ConnectionGroupName = userSessionID;  // We make separate connection-groups for each user session. Within a group connections can be reused.
        req.ServicePoint.ConnectionLimit = 10;    // The default value of 2 within a ConnectionGroup caused me always a "Timeout exception" because a user's 1-3 concurrent WebRequests within a second.
        req.ServicePoint.MaxIdleTime = 5 * 1000;  // (5 sec) default was 100000 (100 sec).  Max idle time for a connection within a ConnectionGroup for reuse before closing
        Log("Statistics: The sum of connections of all connectiongroups within the ServicePoint: " + req.ServicePoint.CurrentConnections; // just for statistics
    
        if (uploadData != null)
        {
            req.ContentType = "application/json";
            SerializeToJson(uploadData, req.GetRequestStream());
        }
        return req;
    }
    
    /// Serializes and writes obj to the requestStream and closes the stream. Uses JSON serialization from System.Runtime.Serialization.        
    public void SerializeToJson(object obj, Stream requestStream)
    {
        DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
        json.WriteObject(requestStream, obj);            
        requestStream.Close();
    }
    

提交回复
热议问题