public async static Task<string> get_http(string orderid)
{
string result = "";
string req = "";
req = string.Format(req_str, orderid);
result = await GetHttpPostStringAsync(req_url, req);
return result;
}
public static async Task<string> GetHttpPostStringAsync(string url, string req)
{
return await Task.Run(() =>
{
return Http_Post(url, req);
});
}
public static string Http_Post(string url, string requestBody)
{
try
{
//设置支持的ssl协议版本,这里我们都勾选上常用的几个
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | (SecurityProtocolType)3072;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/soap+xml;";
string authorization = "XX" + ":" + "XXX";//接口的用户名和密码
string base64 = Convert.ToBase64String(Encoding.Default.GetBytes(authorization));
request.Headers.Add("Authorization", "Basic " + base64);//basic验证
request.CookieContainer = cookieContainer;
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(requestBody);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return "";
}
}
c# HTTP请求(task异步)
最新推荐文章于 2024-06-27 17:08:24 发布