WebClient 通过get和post请求api
2013-11-09 22:22
246 查看
//get 请求
string url = string.Format("http://localhost:28450/api/values?str1=a&str2=b");
WebClient wc = new WebClient();
Encoding enc = Encoding.GetEncoding("UTF-8");
Byte[] pageData = wc.DownloadData(url);
string re = enc.GetString(pageData);
//post 请求
string postData = "value=a";
byte[] bytes = Encoding.UTF8.GetBytes(postData);
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("ContentLength", postData.Length.ToString());
Encoding enc = Encoding.GetEncoding("UTF-8");
byte[] responseData = client.UploadData("http://localhost:28450/api/values", "POST", bytes);
string re = enc.GetString(responseData);
string url = string.Format("http://localhost:28450/api/values?str1=a&str2=b");
WebClient wc = new WebClient();
Encoding enc = Encoding.GetEncoding("UTF-8");
Byte[] pageData = wc.DownloadData(url);
string re = enc.GetString(pageData);
//post 请求
string postData = "value=a";
byte[] bytes = Encoding.UTF8.GetBytes(postData);
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("ContentLength", postData.Length.ToString());
Encoding enc = Encoding.GetEncoding("UTF-8");
byte[] responseData = client.UploadData("http://localhost:28450/api/values", "POST", bytes);
string re = enc.GetString(responseData);
相关文章推荐
- WebClient 通过get和post请求api
- 跨域调用接口——WebClient通过get和post请求api
- 跨域调用接口——WebClient通过get和post请求api
- WebClient 通过get和post请求api
- Loadrunner 11 测试API java Vuser 模拟Post,Get 请求实例
- iOS iPhone通过get和post方式请求asp.net webservice
- java通过Http get post请求url获取数据
- Python3.6通过自带的urllib通过get或post方法请求url的实例
- iphone之通过get和post方式请求asp.net webservice
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- PHP:CURL分别以GET、POST方式请求HTTPS协议接口api
- Android HTTP实例 使用GET方法和POST方法发送请求(通过Apache接口实现)
- 同步和异步Httpclient通过get和post方式发送请求
- javaservlet处理四种常用api请求get,put,post,delete
- android开发学习笔记(一)分别通过GET和POST请求从服务器端获取数据
- 使用api工具时get请求和post请求的不同
- xmlrequest通过GET,POST方法向服务器发送请求
- 通过HttpWebRequest分别向服务器发送GET或POST请求
- koa2快速搭建api服务器-创建路由及定义GET和POST请求
- Android下通过httpClient发送GET和POST请求的实例代码