在我开始之前
首先让我提前道歉,如果这是一个重复的问题,因为似乎有几个与“现有连接被远程主机强制关闭”相关的问题,但是我似乎无法从这些其他问题中获得任何帮助,因为它们相同要么与 FTP(不是 HTTP)有关,似乎是由请求长度问题引起的,或者我根本不明白答案。
目标
我正在尝试使用 System.Net.WebRequest 对第三方服务移动应用程序推送服务Parse.com进行 REST API 调用。尽管这个请求在大约 95% 的时间里都有效,但它会间歇性地抛出一个 System.Net.Web 异常。
我读过的一些类似帖子表明这可能与发送的请求的大小有关,但是这对我来说似乎不太可能,因为每次请求的大小大致相同(注意:这可能是一个无知的假设)。
这是我用来发出 Web 请求的方法
public static WebResponse SendPushHttpRequest(
string payload, string apiID, string apiKey)
{
WebRequest request = WebRequest.Create("https://some.api.com/1/endpoint");
request.ContentType = "application/json";
request.Method = "POST";
request.Headers["Application-Id"] = apiID;
request.Headers["API-Key"] = apiKey;
byte[] buffer =
System.Text.Encoding.GetEncoding("UTF-8").GetBytes(payload);
//string result = Convert.ToBase64String(buffer);
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();
try
{
using (WebResponse response = request.GetResponse())
{
return response;
}
}
catch (Exception ex)
{
throw ex;
}
}
这是引发的 System.Net.Web 异常 底层连接已关闭:接收时发生意外错误。
内部异常 无法从传输连接读取数据:现有连接被远程主机强行关闭。”} System.SystemException {System.IO.IOException}
异常状态 接收失败
堆栈跟踪
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.TlsStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
来自 Web 服务器的响应
GenericParameterAttributes '(((System.Reflection.RuntimeMethodInfo)(((System.Exception)(ex)).TargetSite)).ReturnType).GenericParameterAttributes'
引发了“System.InvalidOperationException”System.Reflection.GenericParameterAttributes {System.InvalidOperationException} 类型的异常
消息:“只能在 Type.IsGenericParameter 为 true 的类型上调用方法。” 细绳
最后但并非最不重要的原始 HTTP 请求
POST http://localhost:58746/mobile/SendPushV2 HTTP/1.1
Host: localhost:58746
Connection: keep-alive
Content-Length: 140
Accept: */*
Origin: http://localhost:58746
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:58746/Mobile
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: .Admin=3B704FD9ECC241F670F57DA43341FBE8A68BDD64F0E863363F31FF75F676EB8AC78D978DE8F1672D34D5C775B49A42E0F6E0F2FB8F867F9F963B0C0AC6C0F1EDD169AB171B8EE0B6A9ACFCBEF1491AAC8FF623DC9874D514EAE7FCF3407C582EBFE0EF4C4A7D495DF38D8F155A2C6275A884BD0F10CFBF6E4B2232641394E9992999755E9F7CD268747DCD7C81A2062DC8D290E99F0A97E0D6594986936DA9BD643D3814EF3621A671103F55EF85E8B648D68E778D1E21D65C22528CE44A3E980A0C2E8BD35B840189A6D9F11B363E69A310A20A7B554FB2C6BFEB7D81784C273DD1DFDBFF2D0FD91A41B522128C1C5156F22FB84007375682013F08372C580A7A2E85C0; ASP.NET_SessionId=nsmooq2pwg04ursg4aly25bs; .AppRoles=bJt49nUK2-dUVVz5AZH2QrG0VHUGrCXpD6FJ3KU4Q5EI9LbvSP1eUCza3g_3dfdkXqgdHJ19C5xVVWO5oByXPFXL5ateafVmbEH9FX5C10pQsOPrGXEe4DZTVzhFd464zydJG5OMCHvO_sTaRFTRAlX5OfyxG6K8J1yL00FnejVZElH51Zm55zhEjbHnkDlxursOZ55skBu9KmxFWe0tgU-9ieuQIo1TUOHe4sTUQec0LyPPaWd7c_cGFjbHqyzoTcVyBf2v86isy5dXaQmIptstKfl8yE6EU1WzVZ15I-N8J3vJKT_ojRKhrDyGyibkb4S0STvGuaqmGAUE27uaBhkafJVkgvP-yU-GA_TMSffq7DWOzezzPldc5LdpxEymFAjeyuzqSc-OVN0cw1uGXm60xgtFnQSUkXMpCFXgYvcUGNb1757gGcTen-zOyBx5KR8-TNE-Z39J3tpljwax_nUDElogSzaQ_9gR5-59ds1KW_LUsQ-quKA1pFqp8SQv-_F_kzRQvIjLsFozGuwkWybvhMpQvg81uPuZsHEIiOEnajIk0
message=Test%5Cn+%5Cn0n77ap&token=0d14611b5ea03c7301bf6aaa0658fb1373192db85c5805aa1058884b9c994839&date=2013-03-19+13%3A41%3A02&platform=ios
如果有人可以帮助我了解这个问题,将不胜感激。