将数据发布到HTTPS网址时,GetRequestStream()会抛出超时异常 [英] GetRequestStream() is throwing time out exception when posting data to HTTPS url

查看:170
本文介绍了将数据发布到HTTPS网址时,GetRequestStream()会抛出超时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用Apache服务器上托管的API来发布数据。我正在使用HttpWebRequest在C#中执行POST。

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

API在服务器上同时具有普通HTTP和安全层(HTTPS)PORT。当我调用HTTP URL时,它完全正常。但是,当我调用HTTPS时,它会给我超时异常(在GetRequestStream()函数中)。任何见解?我正在使用VS 2010,.Net framework 3.5和C#。这是代码块:

API has both normal HTTP and secure layer (HTTPS) PORT on the server. When I call HTTP URL it works perfectly fine. However, when I call HTTPS it gives me time-out exception (at GetRequestStream() function). Any insights? I'm using VS 2010, .Net framework 3.5 and C#. Here is the code block:

string json_value = jsonSerializer.Serialize(data);


        HttpWebRequest 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";

        byte[] buffer = Encoding.ASCII.GetBytes(json_value);
        request.ContentLength = buffer.Length;
        System.IO.Stream reqStream = request.GetRequestStream();
        reqStream.Write(buffer, 0, buffer.Length);
        reqStream.Close();

编辑:
Peter建议的控制台程序运行正常。但是当我添加需要发布到API的数据(以JSON格式)时,它会抛出操作超时异常。这是我添加到基于控制台的应用程序的代码,它会抛出错误。

The console program suggested by Peter works fine. But when I add data (in JSON format) that needs to be posted to the API, it throws out operation timed out exception. Here is the code that I add to console based application and it throws error.

byte[] buffer = Encoding.ASCII.GetBytes(json_value);
request.ContentLength = buffer.Length;


推荐答案

我不知道这是否会对你有帮助您的具体问题,但您应该考虑在完成它们时处理其中的一些对象。我最近正在做这样的事情,并且在使用语句中包装东西似乎为我清理了一堆超时异常。

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

            }

还检查这些东西


  • 服务器是否在本地开发环境中提供https服务?

  • 您是否正确设置了绑定* .443(https)?

  • 您是否需要在请求中设置凭据?

  • 是您的应用程序池帐户访问https资源还是您的帐户正在通过?

  • 您是否考虑过使用WebClient?相反?

  • 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());
        }
    }


编辑:

从控制台发出请求。

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());
            }
        }
    }
}

这篇关于将数据发布到HTTPS网址时,GetRequestStream()会抛出超时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆