【问题标题】:Calling an HTTP handler from C# application throwing exception从 C# 应用程序调用 HTTP 处理程序引发异常
【发布时间】:2015-12-07 12:13:27
【问题描述】:

我正在尝试以与 Chromes Postman 和 Advanced rest 客户端类似的方式将数据从 HTTP 处理程序检索到我的 C# 应用程序。

例如

Target = http://10.113.171.82/handler
Payload =
"<?xml version="1.0" encoding="utf-8"?>
<webpage pageID="406" cmdID="GET" cursor="0"/>"

这在 Postman 或 Advanced Rest Client 中运行良好,但在 C# 应用程序中却惨遭失败。最新的错误是
服务器违反了协议。 Section=ResponseStatusLine。

我已经尝试了通常的建议(KeepAlive False,useUnsafeHeaderParsing="true")。

这是缩略版。

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://10.113.171.82/handler");
string payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?><webpage pageID=\"406\" cmdID=\"GET\" cursor=\"0\"/>";
byte[] data = Encoding.GetEncoding("iso-8859-1").GetBytes(payload);
myHttpWebRequest.ContentLength = data.Length;
myHttpWebRequest.KeepAlive = false;
CookieContainer cookieJar = new CookieContainer();
myHttpWebRequest.ProtocolVersion = HttpVersion.Version11;
myHttpWebRequest.CookieContainer = cookieJar;
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.AllowAutoRedirect = true;
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)";
myHttpWebRequest.Timeout = 100000;
myHttpWebRequest.Accept = "text/xml; charset=utf-8";
myHttpWebRequest.Method = "POST";
myHttpWebRequest.ServicePoint.Expect100Continue = false;
myHttpWebRequest.ServicePoint.MaxIdleTime = 100000;
myHttpWebRequest.KeepAlive = false;
myHttpWebRequest.ContentType = "text/xml; charset=utf-8";
Stream requestStream = myHttpWebRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
myHttpWebRequest.AllowWriteStreamBuffering = true;

//Error on the GetResponse
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

Stream responseStream = myHttpWebResponse.GetResponseStream();

StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);

pageContent = myStreamReader.ReadToEnd();

myStreamReader.Close();
responseStream.Close();

myHttpWebResponse.Close();

注意事项。如果我删除 URLEncoding 错误是“底层连接已关闭:接收时发生意外错误。”虽然这似乎有点命中注定。此外,如果我从目标中删除 /handler,我会得到一个 HTML 响应,但不是我想要的。 已添加大多数属性以尝试找到解决方案。 我想我缺少一些基本的东西!

【问题讨论】:

    标签: c# postman


    【解决方案1】:

    我想我已经发现了问题所在。 服务器根本不返回任何标题。如果我启用源代码调试,我会在 System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)

    虽然这解决了这个问题(为什么会发生),但它只会产生一个新问题。如何在没有 HTTP/1.0 200 OK 的情况下接收 HTTP 请求

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多