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

前端 未结 5 1709
悲&欢浪女
悲&欢浪女 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:25

    Try this:

        WebRequest req = WebRequest.Create("https://server-url-xxxx.com");
        req.Method = "POST";
        string json_value = jsonSerializer.Serialize(data); //Body data
        ServicePointManager.Expect100Continue = false;
        using (var streamWriter = new StreamWriter(req.GetRequestStream()))
        {
            streamWriter.Write(json_value);
            streamWriter.Flush();
            streamWriter.Close();
        }
        HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
        Stream GETResponseStream = resp.GetResponseStream();
        StreamReader sr = new StreamReader(GETResponseStream);
        var response = sr.ReadToEnd(); //Response
        resp.Close(); //Close response
        sr.Close(); //Close StreamReader
    

    And review the URI:

    • Reserved characters. Send reserved characters by the URI can bring problems ! * ' ( ) ; : @ & = + $ , / ? # [ ]

    • URI Length: You should not exceed 2000 characters

提交回复
热议问题