.NET/C#发起GET和POST请求的几种方法

using System.Net;

GET:

1

2

3

var request = (HttpWebRequest)WebRequest.Create("http://www.leadnt.com/recepticle.aspx");

var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

POST:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

var request = (HttpWebRequest)WebRequest.Create("http://www.leadnt.com/recepticle.aspx");

var postData = "thing1=hello";

postData += "&thing2=world";

var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";

request.ContentType = "application/x-www-form-urlencoded";

request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())

{

stream.Write(data, 0, data.Length);

}

var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

第二种:WebClient,也过时了:

1

2

using System.Net;

using System.Collections.Specialized;

GET:

1

2

3

4

using (var client = new WebClient())

{

var responseString = client.DownloadString("http://www.leadnt.com/recepticle.aspx");

}

POST:

1

2

3

4

5

6

7

8

9

10

using (var client = new WebClient())

{

var values = new NameValueCollection();

values["thing1"] = "hello";

values["thing2"] = "world";

var response = client.UploadValues("http://www.leadnt.com/recepticle.aspx", values);

var responseString = Encoding.Default.GetString(response);

}

第三种:HttpClient 当前主流用法,异步请求,自.NET4.5开始可从Nuget包管理中获取。

1

using System.Net.Http;

GET:

1

2

3

4

using (var client = new HttpClient())

{

var responseString = client.GetStringAsync("http://www.mydomain.com/recepticle.aspx");

}

POST:

1

2

3

4

5

6

7

8

9

10

11

12

using (var client = new HttpClient())

{

var values = new List<KeyValuePair<string, string>>();

values.Add(new KeyValuePair<string, string>("thing1", "hello"));

values.Add(new KeyValuePair<string, string>("thing2 ", "world"));

var content = new FormUrlEncodedContent(values);

var response = await client.PostAsync("http://www.mydomain.com/recepticle.aspx", content);

var responseString = await response.Content.ReadAsStringAsync();

}

第四种:第三方类库:

RestSharp

REST API请求测试类库,可通过  NuGet 获得。

Flurl.Http

最新的便捷的api测试工具,使用HttpClient实现,可通过 NuGet 安装。

1

using Flurl.Http;

GET:

1

2

var responseString = await "http://www.mydomain.com/recepticle.aspx"

.GetStringAsync();

POST:

1

2

3

var responseString = await "http://www.mydomain.com/recepticle.aspx"

.PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" })

.ReceiveString();

时间: 2024-08-28 23:59:38

.NET/C#发起GET和POST请求的几种方法的相关文章

php发送get、post请求的6种方法代码示例

本文主要展示了php发送get.post请求的6种方法的代码示例,分别为使用file_get_contents .fopen.fsockopen.curl来发送GET和POST请求,代码如下: 方法1: 用file_get_contents 以get方式获取内容: 1 <?php 2 $url='http://www.jb51.net/'; 3 $html = file_get_contents($url); 4 echo $html; 5 ?> 方法2: 用fopen打开url, 以get方

项目中经常遇到的跨域请求的几种方法

什么是跨域 JSONP proxy代理 cors xdr 关于跨域无非就是jsonp和iframe,随着跨域请求的应用越来越多,W3C提供了跨域请求的标准方案(Cross-Origin Resource Sharing).IE8.Firefox 3.5 及其以后的版本.Chrome浏览器.Safari 4 等已经实现了 Cross-Origin Resource Sharing 规范,实现了跨域请求.在服务器响应客户端的时候,带上Access-Control-Allow-Origin头信息. 如

ASP.NET MVC 实现AJAX跨域请求的两种方法

通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据的加载,例如Google. 在ASP.NET MVC 框架里实现跨域的AJAX请求有几种方式可以实现,以下就介绍常用的两种方法. 1.     发送JSONP请求 客户端: JQuery对发送JSONP请求有很好的支持,客户端通过. ajax() 函数发送请求,其中需要制定 dataType 为“jsonp”  jsonpCallback 为指定的回调函

php发送get、post请求的几种方法

转自:http://blog.csdn.net/haha00217/article/details/7969504 方法1: 用file_get_contents 以get方式获取内容 1 <?php 2 $url='http://www.domain.com/'; 3 $html = file_get_contents($url); 4 echo $html; 5 ?> 方法2: 用fopen打开url, 以get方式获取内容 1 <?php 2 $fp = fopen($url, '

实现跨域请求的4种方法

模拟服务器端的PHP文件: service: <?php//允许访问header('Access-Control-Allow-Origin:*');@$callback=$_GET['callback'];//创建数据$userInfo = array('id'=>1,'username'=>'Scott Jeremy','email'=>'[email protected]');//编译成JSON$result = json_encode($userInfo);echo $cal

php发送post请求的三种方法

引用:http://blog.sjzycxx.cn/post/435/ 1.使用 file_get_contents() /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data post键值对数据 * @return string */function send_post($url, $post_data) { $postdata = http_build_query($post_data); $options = a

(转)php发送get、post请求的几种方法

方法1: 用file_get_contents 以get方式获取内容 1 <?php 2 $url='http://www.domain.com/'; 3 $html = file_get_contents($url); 4 echo $html; 5 ?> 方法2: 用fopen打开url, 以get方式获取内容 1 <?php 2 $fp = fopen($url, 'r'); 3 //返回请求流信息(数组:请求状态,阻塞,返回值是否为空,返回值http头等) 4 stream_ge

php发送http请求的几种方法

有很多时候,我们还是需要用php去发送http请求的,它可以模拟浏览器的行为,通常它的应用场景有:1.后端测试自己的接口.2.后端请求别人的数据. 后端测试自己的接口,比如我们写了一个返回json数据的接口,我们可以让前端去测,但是前端不一定有空啊,或者前端界面还没做出来,由于界面不一定是现成的,因此测试起来也会有点麻烦.当然我们可以用谷歌浏览器的postman或者火狐的poster,这些都可以发送post.delete请求等等,但是它们应用起来并没有那么爽.因此,我们可以实用php编程的方式来

vue中数据请求的三种方法

注意请求可能存在跨域问题,需要去配置好 这三种建议使用axios 1.resource Vue 要实现异步加载需要使用到 vue-resource 库. Vue.js 2.0 版本推荐使用 axios 来完成 ajax 请求. 先导入一个线上cdn的地址,当然还可以去npm安装,但个人觉得这种方便 <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></scri