HttpClient实现 get、post、put、delete请求

public static String doGet(String url) {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		String result = "";
		try {
			// 通过址默认配置创建一个httpClient实例
			httpClient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(url);
			// 设置请求头信息,鉴权
			// httpGet.setHeader("Authorization", "Bearer
			// da3efcbf-0845-4fe3-8aba-ee040be542c0");
			// 设置配置请求参数
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
					.setConnectionRequestTimeout(35000)// 请求超时时间
					.setSocketTimeout(60000)// 数据读取超时时间
					.build();
			// 为httpGet实例设置配置
			httpGet.setConfig(requestConfig);
			// 执行get请求得到返回对象
			response = httpClient.execute(httpGet);
			// 通过返回对象获取返回数据
			HttpEntity entity = response.getEntity();
			// 通过EntityUtils中的toString方法将结果转换为字符串
			result = EntityUtils.toString(entity);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static String doPost(String url, String json) {
		System.out.println(url);
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		String result = "";
		try {
			// 通过址默认配置创建一个httpClient实例
			httpClient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);
			System.out.println(json);
			StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
			httpPost.setEntity(stringEntity);

			// 设置请求头信息,鉴权
			// httpGet.setHeader("Authorization", "Bearer
			// da3efcbf-0845-4fe3-8aba-ee040be542c0");
			// 设置配置请求参数
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
					.setConnectionRequestTimeout(35000)// 请求超时时间
					.setSocketTimeout(60000)// 数据读取超时时间
					.build();
			// 为httpGet实例设置配置
			httpPost.setConfig(requestConfig);
			// 执行get请求得到返回对象
			response = httpClient.execute(httpPost);
			// 通过返回对象获取返回数据
			HttpEntity entity = response.getEntity();
			// 通过EntityUtils中的toString方法将结果转换为字符串
			result = EntityUtils.toString(entity);
			System.out.println(result);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static String doPut(String url, String json) {
		System.out.println(url);
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		String result = "";
		try {
			// 通过址默认配置创建一个httpClient实例
			httpClient = HttpClients.createDefault();
			HttpPut httpPut = new HttpPut(url);
			System.out.println(json);
			StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
			httpPut.setEntity(stringEntity);

			// 设置请求头信息,鉴权
			// httpGet.setHeader("Authorization", "Bearer
			// da3efcbf-0845-4fe3-8aba-ee040be542c0");
			// 设置配置请求参数
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
					.setConnectionRequestTimeout(35000)// 请求超时时间
					.setSocketTimeout(60000)// 数据读取超时时间
					.build();
			// 为httpGet实例设置配置
			httpPut.setConfig(requestConfig);
			// 执行get请求得到返回对象
			response = httpClient.execute(httpPut);
			// 通过返回对象获取返回数据
			HttpEntity entity = response.getEntity();
			// 通过EntityUtils中的toString方法将结果转换为字符串
			result = EntityUtils.toString(entity);
			System.out.println(result);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static String doDelete(String url) {
		System.out.println(url);
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		String result = "";
		try {
			// 通过址默认配置创建一个httpClient实例
			httpClient = HttpClients.createDefault();
			HttpDelete httpDelete = new HttpDelete(url);
			// 设置请求头信息,鉴权
			// httpGet.setHeader("Authorization", "Bearer
			// da3efcbf-0845-4fe3-8aba-ee040be542c0");
			// 设置配置请求参数
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
					.setConnectionRequestTimeout(35000)// 请求超时时间
					.setSocketTimeout(60000)// 数据读取超时时间
					.build();
			// 为httpGet实例设置配置
			httpDelete.setConfig(requestConfig);
			// 执行get请求得到返回对象
			response = httpClient.execute(httpDelete);
			// 通过返回对象获取返回数据
			HttpEntity entity = response.getEntity();
			// 通过EntityUtils中的toString方法将结果转换为字符串
			result = EntityUtils.toString(entity);
			System.out.println(result);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

 

1. GET方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送GET请求 HttpResponseMessage response = await client.GetAsync("http://www.example.com"); string responseBody = await response.Content.ReadAsStringAsync(); ``` 2. POST方法 ```csharp using System.Net.Http; using System.Text; using System.Text.Json; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 准备POST请求数据 var data = new { name = "John", age = 30 }; var jsonContent = JsonSerializer.Serialize(data); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); // 发送POST请求 HttpResponseMessage response = await client.PostAsync("http://www.example.com", content); string responseBody = await response.Content.ReadAsStringAsync(); ``` 3. PUT方法 ```csharp using System.Net.Http; using System.Text; using System.Text.Json; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 准备PUT请求数据 var data = new { name = "John", age = 30 }; var jsonContent = JsonSerializer.Serialize(data); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); // 发送PUT请求 HttpResponseMessage response = await client.PutAsync("http://www.example.com", content); string responseBody = await response.Content.ReadAsStringAsync(); ``` 4. DELETE方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送DELETE请求 HttpResponseMessage response = await client.DeleteAsync("http://www.example.com"); string responseBody = await response.Content.ReadAsStringAsync(); ``` 5. HEAD方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送HEAD请求 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, "http://www.example.com"); HttpResponseMessage response = await client.SendAsync(request); ``` 6. OPTIONS方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送OPTIONS请求 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Options, "http://www.example.com"); HttpResponseMessage response = await client.SendAsync(request); ``` 7. TRACE方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送TRACE请求 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Trace, "http://www.example.com"); HttpResponseMessage response = await client.SendAsync(request); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值