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

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

    I don't know if this will help you with your specific problem but you should consider Disposing some of those objects when you are finished with them. I was doing something like this recently and wrapping stuff up in using statements seems to clean up a bunch of timeout exceptions for me.

                using (var reqStream = request.GetRequestStream())
                {
                    if (reqStream == null)
                    {
                        return;
                    }
    
                  //do whatever
    
                }
    

    also check these things

    • Is the server serving https in your local dev environment?
    • Have you set up your bindings *.443 (https) properly?
    • Do you need to set credentials on the request?
    • Is it your application pool account accessing the https resources or is it your account being passed through?
    • Have you thought about using WebClient instead?

      using (WebClient client = new WebClient())
          {               
              using (Stream stream = client.OpenRead("https://server-url-xxxx.com"))
              using (StreamReader reader = new StreamReader(stream))
              {
                  MessageBox.Show(reader.ReadToEnd());
              }
          }
      

    EDIT:

    make a request from console.

    internal class Program
    {
        private static void Main(string[] args)
        {
            new Program().Run();
            Console.ReadLine();
        }
    
        public void Run()
        {
    
           var request = (HttpWebRequest)System.Net.WebRequest.Create("https://server-url-xxxx.com");
            request.Method = "POST";
            request.ProtocolVersion = System.Net.HttpVersion.Version10;
            request.ContentType = "application/x-www-form-urlencoded";
    
            using (var reqStream = request.GetRequestStream())
            {
                using(var response = new StreamReader(reqStream )
                {
                  Console.WriteLine(response.ReadToEnd());
                }
            }
        }
    }
    

提交回复
热议问题