.Net 请求Web接口Post和Get方法
2017-07-12 11:07
519 查看
#region web服务请求 get post
static string DefaultUserAgent = "www.zhiweiworld.com";
public static String Get(string url)
{
System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
request.Method = "GET";
request.UserAgent = DefaultUserAgent;
System.Net.HttpWebResponse result = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader sr = new System.IO.StreamReader(result.GetResponseStream(), Encoding.UTF8);
string strResult = sr.ReadToEnd();
sr.Close();
//Console.WriteLine(strResult);
return strResult;
}
public static String Post(string url, System.Collections.Specialized.NameValueCollection para)
{
System.Net.WebClient WebClientObj = new System.Net.WebClient();
byte[] byRemoteInfo = WebClientObj.UploadValues(url, "POST", para);//请求地址,传参方式,参数集合
string rtContent = System.Text.Encoding.UTF8.GetString(byRemoteInfo);//获取返回值
return rtContent;
}
#endregion
static string DefaultUserAgent = "www.zhiweiworld.com";
public static String Get(string url)
{
System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
request.Method = "GET";
request.UserAgent = DefaultUserAgent;
System.Net.HttpWebResponse result = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader sr = new System.IO.StreamReader(result.GetResponseStream(), Encoding.UTF8);
string strResult = sr.ReadToEnd();
sr.Close();
//Console.WriteLine(strResult);
return strResult;
}
public static String Post(string url, System.Collections.Specialized.NameValueCollection para)
{
System.Net.WebClient WebClientObj = new System.Net.WebClient();
byte[] byRemoteInfo = WebClientObj.UploadValues(url, "POST", para);//请求地址,传参方式,参数集合
string rtContent = System.Text.Encoding.UTF8.GetString(byRemoteInfo);//获取返回值
return rtContent;
}
#endregion
相关文章推荐
- .Net 请求Web接口Post和Get方法
- 关于微信 init接口返回的Count和webwxbatchgetcontact post请求的参数Count
- loadrunner通过web的post请求方法测接口
- HTTP请求接口方法(POST/GET)
- .NET/C#发起GET和POST请求的几种方法
- web接口测试之GET与POST请求
- .NET/C#发起GET和POST请求的几种方法
- web接口测试之GET与POST请求
- curl请求接口的get和post方法
- loadrunner通过web的post请求方法测接口 2
- web接口Flask测试之GET与POST请求
- loadrunner通过web的post请求方法测接口 1
- Android HTTP实例 使用GET方法和POST方法发送请求(通过Apache接口实现)
- 【Java web】get、put、post、delete请求方法总结
- .NET获取聚合数据接口数据(Http协议POST和GET请求)
- ajax的post请求如何让web或接口的方法支持跨域()
- Java Web开发8__HTTP GET请求、POST请求的区别, 重写 service() 方法
- web接口测试GET与POST请求
- android利用httpclient实现post、get请求restful接口进行json和form表单数据提交等公共方法类