WebClient. DownLoadString error: the connection was accidentally closed,
Call the DownLoadString method of WebClient to call the interface. When the data volume is small (more than a dozen pieces of data), everything works normally. Later, the other party suddenly put more than one thousand pieces of data, and then reported an error: the connection was accidentally closed.
First, the connection object is closed when the peer interface is not output. This possibility is denied after troubleshooting. It is called through HttpWebRequest and then cyclically reads the byte stream. No error is reported, but the reading is incomplete.
Finally, modify it to this method to solve the problem:
Public string GetManyData (string url)
{
HttpWebRequest request = (HttpWebRequest) HttpWebRequest. Create (url );
Request. ContentType = "application/json ";
Request. Accept = "application/json, text/javascript, */*, q = 0.01 ";
Request. Headers. Add ("Accept-Encoding", "deflate, gzip ");
Request. AutomaticDecompression = DecompressionMethods. Deflate | DecompressionMethods. GZip | DecompressionMethods. None;
Using (Stream stream = request. GetResponse (). GetResponseStream ())
{
StreamReader sr = new StreamReader (stream );
Return sr. ReadToEnd ();
}
}
The internal reasons have not been carefully studied.