如何在WinForm中发送HTTP请求
1个回答
展开全部
模拟一个http请求就是了:
public static string Post(string url, string requestContont)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream sw = null;
StreamReader sr = null;
try
{
ServicePointManager.DefaultConnectionLimit = 200;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidate);
request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";// 请求方式:POST或GET
byte[] bytes = Encoding.UTF8.GetBytes(requestContont);
request.ContentLength = bytes.Length;
sw = request.GetRequestStream();
sw.Write(bytes, 0, bytes.Length);
sw.Flush();
response = request.GetResponse() as HttpWebResponse;
string status = ((HttpWebResponse)response).StatusCode.ToString();
sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string responseData = sr.ReadToEnd();
return responseData;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sw != null)
{
sw.Close();
sw.Dispose();
}
if (request != null)
{
request.Abort();
request = null;
}
if (response != null)
{
response.Close();
response = null;
}
if (sr != null)
{
sr.Close();
sr.Dispose();
}
}
}
public static string Post(string url, string requestContont)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream sw = null;
StreamReader sr = null;
try
{
ServicePointManager.DefaultConnectionLimit = 200;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidate);
request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";// 请求方式:POST或GET
byte[] bytes = Encoding.UTF8.GetBytes(requestContont);
request.ContentLength = bytes.Length;
sw = request.GetRequestStream();
sw.Write(bytes, 0, bytes.Length);
sw.Flush();
response = request.GetResponse() as HttpWebResponse;
string status = ((HttpWebResponse)response).StatusCode.ToString();
sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string responseData = sr.ReadToEnd();
return responseData;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sw != null)
{
sw.Close();
sw.Dispose();
}
if (request != null)
{
request.Abort();
request = null;
}
if (response != null)
{
response.Close();
response = null;
}
if (sr != null)
{
sr.Close();
sr.Dispose();
}
}
}
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询