将数据发布到HTTPSurl时,GetRequestStream()会抛出超时exception

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

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

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

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

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

我不知道这是否可以帮助您解决具体问题,但是您应该考虑在完成这些对象后对其进行处理。 我最近正在做这样的事情,并且在使用语句中包装东西似乎为我清理了一堆超时exception。

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

还检查这些东西

  • 服务器是否在本地开发环境中提供https服务?
  • 您是否正确设置了绑定* .443(https)?
  • 您是否需要在请求上设置凭据?
  • 是您的应用程序池帐户访问https资源还是您的帐户正在通过?
  • 您是否考虑过使用WebClient?

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

我遇到了同样的问题。 好像它已经为我解决了。 我查看了所有代码,确保为所有HttpWebResponse对象调用webResponse.Close()和/或responseStream.Close() 。 该文档表明您可以关闭流或HttpWebResponse对象。 调用两者都没有害处,所以我做到了。 不关闭响应可能会导致应用程序耗尽连接以便重用,这似乎会影响HttpWebRequest.GetRequestStream,就我在代码中可以看到的而言。

试试这个:

  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 

并查看URI:

  • 保留字符。 通过URI发送保留字符会带来问题! * ' ( ) ; : @ & = + $ , / ? # [ ] ! * ' ( ) ; : @ & = + $ , / ? # [ ]

  • URI长度:不应超过2000个字符

您可能需要设置超时属性,请在此处查看http://www.codeproject.com/Tips/69637/Setting-timeout-property-for-System-Net-WebClient