HttpClient 请求
2018-03-13 11:01
176 查看
一、.net2.0 自带的WebClient请求,不带cookie,需要从写
1、post请求
/// <summary>
/// Posts the specified URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="data">The data.</param>
/// <param name="contentType">Type of the content.</param>
/// <param name="headers">The headers.</param>
/// <returns></returns>
public static string Post(string url, string data, string contentType, NameValueCollection headers = null)
{
string result = "";
try
{
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = contentType;
client.Headers[HttpRequestHeader.Accept] = " text/html, */*";
client.Headers[HttpRequestHeader.AcceptCharset] = "GBK";
client.Headers[HttpRequestHeader.AcceptLanguage] = "zh-cn";
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/3.0 (compatible; Indy Library)";
if (headers != null)
{
for (int i = 0; i < headers.Count; i++)
{
client.Headers.Set(headers.AllKeys[i], headers[i]);
}
}
result = client.UploadString(url, "POST", data);
}
}
catch (WebException wex)
{
throw wex;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
2、get 请求
public static string GetData(string url, NameValueCollection headers = null)
{
string result = "";
try
{
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
if (headers != null)
{
for (int i = 0; i < headers.Count; i++)
{
client.Headers.Set(headers.AllKeys[i], headers[i]);
}
}
result = client.DownloadString(url);
}
}
catch (WebException wex)
{
throw wex;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
二、httpclient 请求,自带cookie。
1、get 请求
var s = http.GetStringAsync(geturl).Result;
2、post请求
Template template = new Template();
template.TemplateContent = Properties.Resources.EnterpriseHead;
template.Set("nsr", nsr);
template.Set("djxh", DjxhChache[nsrsbh]);
string tycxparam = template.Render();
Dictionary<string, string> dictionary = new Dictionary<string, string>()
{
{ "bizXml",tycxparam},
{ "sid","id"},
{ "action","queryData"}
};
var request = http.PostAsync(url, new FormUrlEncodedContent(dictionary)).Result;
1、post请求
/// <summary>
/// Posts the specified URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="data">The data.</param>
/// <param name="contentType">Type of the content.</param>
/// <param name="headers">The headers.</param>
/// <returns></returns>
public static string Post(string url, string data, string contentType, NameValueCollection headers = null)
{
string result = "";
try
{
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = contentType;
client.Headers[HttpRequestHeader.Accept] = " text/html, */*";
client.Headers[HttpRequestHeader.AcceptCharset] = "GBK";
client.Headers[HttpRequestHeader.AcceptLanguage] = "zh-cn";
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/3.0 (compatible; Indy Library)";
if (headers != null)
{
for (int i = 0; i < headers.Count; i++)
{
client.Headers.Set(headers.AllKeys[i], headers[i]);
}
}
result = client.UploadString(url, "POST", data);
}
}
catch (WebException wex)
{
throw wex;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
2、get 请求
public static string GetData(string url, NameValueCollection headers = null)
{
string result = "";
try
{
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
if (headers != null)
{
for (int i = 0; i < headers.Count; i++)
{
client.Headers.Set(headers.AllKeys[i], headers[i]);
}
}
result = client.DownloadString(url);
}
}
catch (WebException wex)
{
throw wex;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
二、httpclient 请求,自带cookie。
1、get 请求
var s = http.GetStringAsync(geturl).Result;
2、post请求
Template template = new Template();
template.TemplateContent = Properties.Resources.EnterpriseHead;
template.Set("nsr", nsr);
template.Set("djxh", DjxhChache[nsrsbh]);
string tycxparam = template.Render();
Dictionary<string, string> dictionary = new Dictionary<string, string>()
{
{ "bizXml",tycxparam},
{ "sid","id"},
{ "action","queryData"}
};
var request = http.PostAsync(url, new FormUrlEncodedContent(dictionary)).Result;
相关文章推荐
- 【Angular】angular-HttpClient 与服务器通讯 Get Post Put Delete Http 拦截器 请求讲解
- HttpClient发起post请求
- C# HttpWebRequest\HttpWebResponse\WebClient发送请求解析json数据
- HttpClientUtil发送json格式请求
- WebApi 异步请求(HttpClient)
- 在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决
- httpClient发送post请求
- Android之Http通信——4.Android HTTP请求方式:HttpClient
- http请求开源框架: AsyncHttpClient
- HttpClient通过代理进行请求
- fiddler 抓不到app包 抓不到okhttp/asynchttpclient的网络请求
- httpclient、Nginx、tomcat之间请求长连接一致问题
- Httpclient 请求网络
- HttpClient发起GET或者POST请求
- httpClient发送https请求
- HttpClient服务端的请求
- Android 网络请求HttpURLConnection 和 HttpClient详解
- 用httpclient4.x 发送http get post请求。
- httpclient 实现https请求
- Android Asynchronous Http Client-Android异步网络请求客户端接口