我得到了这个例外。
The underlying connection was closed: An unexpected error occurred on a send.
我已经尝试了很多方法(ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 和 keepalive 方法)。问题仍然没有解决。谁能帮我解决问题?
public static string FunGetURLShortening(string strLargeURL)
{
string strShortURL = "";
string strAPI = ConfigurationManager.AppSettings["API"].ToString();
//var request = (HttpWebRequest)WebRequest.Create(strAPI);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strAPI);
request.KeepAlive = true;
string strName = ConfigurationManager.AppSettings["ReqParaName"].ToString();
string strReqParameter = "name=" + strName + "&" + "url=" + strLargeURL;
byte[] data = Encoding.UTF8.GetBytes(strReqParameter);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.ProtocolVersion = HttpVersion.Version10;
//ServicePointManager.Expect100Continue = true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
//ServicePointManager.SecurityProtocol = (SecurityProtocolType)48 | (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
//| SecurityProtocolType.Tls;
//System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)0x00000C00;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
try
{
var response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
JavaScriptSerializer serial = new JavaScriptSerializer();
serial.MaxJsonLength = Int32.MaxValue;
var ResponsePara = serial.Deserialize<ResponseParameters>(responseString);
if (ResponsePara.status == "1")
{
strShortURL = ResponsePara.data;
}
else
{
strShortURL = strLargeURL;
}
}
catch (Exception ex)
{
ErrorLog.WriteErrorLog(strLogFilePath, "Error:- " + ex.Message.ToString());
}
return strShortURL;
}