Home > Net >  C # HTTPS post when receiving response respose when tip "unable to read data from the transport
C # HTTPS post when receiving response respose when tip "unable to read data from the transport

Time:09-18

Code is this: how to solve it,
Public R HttpPostJson (string url, List T, string projectCode=", "string hosCode=" ", string hosName="", string timespan=" ", string key="")
T: where a class where R: class
{
ProjectCode=ElecInfo. ProjectCode;
HosCode=ElecInfo. HosCode;
HosName=ElecInfo. HosName;
Key=ElecInfo. Key;
//timestamp
Timespan=CommonHelper. GetCurrentTimeStamp (). The ToString ();

# region log

//write and log
LoggerHelper. Write (the string. Format (" \ r \ n time: {0}, \ r \ n address: {1}, \ r \ n interface: {2}, \ r \ n content: {3} \ r \ n ",
DateTime. Now, the url, t.T oString (). The Split ('. ') [t.T oString (). The Split ('. '). The Length - 1]. TrimEnd (']),
ListToJsonSerializer (t)), LogsType InParm);
# endregion

Try
{
ServicePointManager. DefaultConnectionLimit=100;
///based on the URL to create a HttpWebRequest object
HttpWebRequest httpWeb=(HttpWebRequest) HttpWebRequest. Create (url);
//set the request way as the post request
HttpWeb. Method="POST";
//httpWeb. Accept="HTTP";
HttpWeb. ContentType="application/json; Charset=utf-8 ";

//json content
//1. Assemble the header content
SetHeaderValue (httpWeb Headers, "the content-type", "application/json");
SetHeaderValue (httpWeb Headers, "projectCode" projectCode);
SetHeaderValue (httpWeb Headers, "hosCode", hosCode);
SetHeaderValue (httpWeb Headers, hosName, hosName);

//2. Get the header encryptData encrypted data
Var encryptData=https://bbs.csdn.net/topics/GetHeaderEncryption (timespan);
If (string. IsNullOrWhiteSpace (encryptData)) return null;
SetHeaderValue (httpWeb Headers, "encryptData", encryptData);

//3. The assembly body section
StringBuilder stringBuilder=new StringBuilder();
//the serialized instance t
Var body=ListToJsonSerializer (t);

Var dict=new SortedDictionary
{
{" key ", ElecInfo. Key},
{" originaldata, "body},
};

//encryption body subject
Var result=GetBodyEncryption (dict);

//convert json to
Byte [] buffer.=Encoding UTF8. GetBytes (result);
HttpWeb. ContentLength=buffer. Length;
HttpWeb. UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; The.net CLR 1.0.3705;) ";
HttpWeb. Connection="";
HttpWeb. KeepAlive=false;

Using (Stream Stream.=httpWeb GetRequestStream ())
{
Stream. Write (buffer, 0, buffer Length);
Stream. The Close ();
}

//httpWeb here. The method GetResponse () when the tip "unable to read data from the transport connection: the connection is closed,"
Using (HttpWebResponse response=httpWeb. The method GetResponse () as HttpWebResponse)
{
Using (StreamReader reader=new StreamReader (response. GetResponseStream (), Encoding, UTF8))
{
//get the cipher decryption returns json data
R R=JsonDeserializeObject (GetDecrypt (reader. ReadToEnd ()));

# region log
//write and log
LoggerHelper. Write (the string. Format (" time: {0}, \ r \ n address: {1}, \ r \ n interface: {2}, \ r \ n content: {3} \ r \ n ",
DateTime. Now, the url, t.T oString (). The Split ('. ') [t.T oString (). The Split ('. '). The Length - 1]. TrimEnd (']),
GetDecrypt (reader. ReadToEnd ())), LogsType. OutParm);
# endregion

return r;
}
}
}
The catch (Exception ex)
{
# region log

//write error log
LoggerHelper. Write (the string. Format (" time: {0}, \ r \ n address: {1}, \ r \ n interface: {2}, \ r \ n content: {3} \ r \ n ",
DateTime. Now, the url, t.T oString (). The Split ('. ') [t.T oString (). The Split ('. '). The Length - 1]. TrimEnd (']),
Ex. The Message. The ToString ()), LogsType. Error);
# endregion

Return JsonDeserializeObject (JsonSerializeObject (ex. Message));
}
}
  •  Tags:  
  • C #
  • Related