C#使用HttpClient进行GET&POST
2014-06-07 17:09
447 查看
就放两个代码过来吧,稍微做个记录
这是通过HttpClient.GetAsync(uri)进行HTTP数据获取的方式,验证参数似乎需要添加在uri之后,所以这段代码在实际应用中需要进行部分更改,将uri及验证信息作为函数输入
public async void HttpClientDoGet()
{
var uri = "http://api.wsncloud.com/device/v1/list?";
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None };
using (var httpclient = new HttpClient(handler))
{
httpclient.BaseAddress = new Uri(uri);
httpclient.DefaultRequestHeaders.Accept.Clear();
httpclient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpclient.GetAsync("?ak=xxxxxxxxxxxxxxxxxxxx");
if(response.IsSuccessStatusCode)
{
Stream myResponseStream = await response.Content.ReadAsStreamAsync();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
MessageBox.Show(retString);
}
}
}这个则为通过HttpClient.PostAsync(ur1, content)获取HTTP数据的方法
public async void HttpClientDoPost()
{
var uri = "http://api.wsncloud.com/sensor/v1/list?";
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None };
using (var httpclient = new HttpClient(handler))
{
httpclient.BaseAddress = new Uri(uri);
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"ak", "65fc7ca4fc441d26f71bf3d691b278c2"},
{"deviceId", "537eb34be4b022b7fbe19471"}
});
var response = await httpclient.PostAsync(uri, content);
string responseString = await response.Content.ReadAsStringAsync();
MessageBox.Show(responseString);
}
}Post和Get命令在实际使用中都可以用来获取服务器端的参数
这是通过HttpClient.GetAsync(uri)进行HTTP数据获取的方式,验证参数似乎需要添加在uri之后,所以这段代码在实际应用中需要进行部分更改,将uri及验证信息作为函数输入
public async void HttpClientDoGet()
{
var uri = "http://api.wsncloud.com/device/v1/list?";
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None };
using (var httpclient = new HttpClient(handler))
{
httpclient.BaseAddress = new Uri(uri);
httpclient.DefaultRequestHeaders.Accept.Clear();
httpclient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpclient.GetAsync("?ak=xxxxxxxxxxxxxxxxxxxx");
if(response.IsSuccessStatusCode)
{
Stream myResponseStream = await response.Content.ReadAsStreamAsync();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
MessageBox.Show(retString);
}
}
}这个则为通过HttpClient.PostAsync(ur1, content)获取HTTP数据的方法
public async void HttpClientDoPost()
{
var uri = "http://api.wsncloud.com/sensor/v1/list?";
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None };
using (var httpclient = new HttpClient(handler))
{
httpclient.BaseAddress = new Uri(uri);
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"ak", "65fc7ca4fc441d26f71bf3d691b278c2"},
{"deviceId", "537eb34be4b022b7fbe19471"}
});
var response = await httpclient.PostAsync(uri, content);
string responseString = await response.Content.ReadAsStringAsync();
MessageBox.Show(responseString);
}
}Post和Get命令在实际使用中都可以用来获取服务器端的参数
相关文章推荐
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等<转>
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- (转)HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- TTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- 【转】HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- [zt] HTTP 协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等【转】
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等