HttpClient实现请求

 1  /// <summary>
 2         ///     HttpClient实现Get请求(异步)
 3         /// </summary>
 4         private static async void DoGet()
 5         {
 6             var url = "http://localhost:5555/api/Test/Get?id=1";
 7             //创建HttpClient(注意传入HttpClientHandler)
 8             var handler = new HttpClientHandler {AutomaticDecompression = DecompressionMethods.GZip};
 9
10             using (var http = new HttpClient(handler))
11             {
12                 //await异步等待回应
13                 var response = await http.GetAsync(url);
14                 //确保HTTP成功状态值
15                 response.EnsureSuccessStatusCode();
16
17                 //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
18                 Console.WriteLine(await response.Content.ReadAsStringAsync());
19             }
20         }
21
22         /// <summary>
23         ///     HttpClient实现Put请求(异步)
24         /// </summary>
25         private static async void DoPut()
26         {
27             var userId = 6;
28             var url = "http://localhost:5555/api/put/register?userid=" + userId;
29
30             //设置HttpClientHandler的AutomaticDecompression
31             var handler = new HttpClientHandler {AutomaticDecompression = DecompressionMethods.GZip};
32             //创建HttpClient(注意传入HttpClientHandler)
33             using (var http = new HttpClient(handler))
34             {
35                 //使用FormUrlEncodedContent做HttpContent
36                 var content = new FormUrlEncodedContent(new Dictionary<string, string>
37                 {
38                     {"UserName", "修改胡景宝"},
39                     {"UserEmail", "[email protected]"}
40                 });
41
42                 //await异步等待回应
43
44                 var response = await http.PutAsync(url, content);
45                 //确保HTTP成功状态值
46                 response.EnsureSuccessStatusCode();
47                 //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
48                 Console.WriteLine(await response.Content.ReadAsStringAsync());
49             }
50         }
时间: 2024-10-26 17:34:23

HttpClient实现请求的相关文章

【黑马Android】(06)使用HttpClient方式请求网络/网易新闻案例

使用HttpClient方式请求网络 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"

java HttpClient POST请求

一个简单的HttpClient POST 请求实例 package com.httpclientget; import java.awt.List; import java.util.ArrayList; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost

java HttpClient GET请求

HttpClient GET请求小实例,先简单记录下. package com.httpclientget; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient

用HttpPost 和 HttpClient 发送请求到web 端回调数据

btnok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 跳转到新的页面 String name=usernameTextId.getText().toString(); String pwd=passwordTextId.getText().toString(); String url = "http://112.124.12.46/wxtest/login.soap?

httpclient + AsyncTask 请求数据 || httpclient + handler 请求数据

public class MyAsy extends AsyncTask<String, Integer, String> { private String json; @Override    protected String doInBackground(String... params) {        // TODO Auto-generated method stub        // 实例化HttpClient        HttpClient client = new De

httpclient post 请求

package com.thinkgem.jeesite.common.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; imp

HttpClient Post请求

doPost(null, "https://www.baidu.com/"); /** * 访问数据库并返回JSON数据字符串 * * @param params * 向服务器端传的参数 * @param url * @return * @throws Exception */ public static String doPost(List<NameValuePair> params, String url) throws Exception { String resul

httpClient多线程请求

使用httpClient可模拟请求Url获取资源,使用单线程的请求速度上会有一定的限制,参考了Apache给出的例子,自己做了测试实现多线程并发请求,以下代码需要HttpClient 4.2的包,可以在http://hc.apache.org/downloads.cgi下载 1.并发请求 package generate.httpclient; import java.util.List; import java.util.concurrent.ExecutorService; import j

使用HttpClient实现请求转发功能

由于移动端要显示业务系统的数据,但是业务系统众多,如果直接和业务系统交互,会紧耦合,杂乱无章,不好管理,特编写请求转发组件,实现统一中转. 本文基于HttpClient实现了GET和POST两种方式: GET请求: /** * 根据GET方式请求获取请求内容 * @param url * @return */ private String getContentFromUrlByGet(String url) { String content = ""; try { HttpClient