HttpClient工具

当写好了WEB程序,使用Java客户端访问,最好使用HttpComponents组件。在apche上下载,配置到项目中。

范例:定义一个处理用户请求的Servlet程序

package cn.ren.demo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@WebServlet("/hello")
public class HttpServlet extends javax.servlet.http.HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().println("name = " + request.getParameter("name")); 
		System.out.println("***" + request.getParameter("name")); // 在后台做个打印
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}

范例:定义一个Get请求:

package cn.ren.demo;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;

public class HttpClientGetDemo {
	public static void main(String[] args) throws Exception {
		String url = "http://localhost/HttpServlet/hello?name=ren" ;
		CloseableHttpClient httpClient = HttpClients.createDefault() ;  // 创建一个Http访问客户端
		HttpGet httpGet = new HttpGet(url) ;  // GET请求
		CloseableHttpResponse reponse = httpClient.execute(httpGet) ; // 发送GET请求
		System.out.println(reponse.getEntity());
		System.out.println(reponse.getCode());
		System.out.println(EntityUtils.toString(reponse.getEntity()));
	}
}

范例:使用HttpClient发送POST请求

在HelloServlet里面追加一个处理POST请求的方法:

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String msg = request.getParameter("msg") ;
		System.out.println("### POST请求" + msg); // 后台打印
		response.getWriter().print("<h1>" + msg + "</h1>");
	}

定义POST请求处理:

package cn.ren.demo;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;

public class HttpClientPostDemo {
	public static void main(String[] args) throws Exception {
		String url = "http://localhost/HttpServlet/hello";
		CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建一个Http访问客户端
		HttpPost httpPost = new HttpPost(url); // Post请求
		// 需要将所有的参数进行一个封装处理
		List<NameValuePair> allParams = new ArrayList<NameValuePair>();
		allParams.add(new BasicNameValuePair("msg", "世界,你好")); // 追加传递参数
		allParams.add(new BasicNameValuePair("mid", "renjava"));
		// 由于传递了中文,所以需要对中文进行编码控制
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(allParams); // 设置编码
		httpPost.setEntity(entity); // 将发送的数据与POST对象绑定在一起
		CloseableHttpResponse response = httpClient.execute(httpPost); // 发送Post请求
		System.out.println(response.getEntity());
		System.out.println(response.getCode());
		System.out.println(EntityUtils.toString(response.getEntity()));
	}
}

 

HttpClient 是一个开源的 HTTP 客户端库,用于发送 HTTP 请求和处理响应。它是 Apache HttpComponents 项目的一部分,提供了简单且灵活的 API,用于进行 HTTP 通信。 你可以使用 HttpClient 来执行各种 HTTP 操作,如发送 GET、POST、PUT、DELETE 等请求。它支持处理响应、处理重定向、处理认证、处理代理等功能。 以下是使用 HttpClient 发送 GET 请求的示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet("http://example.com"); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity); System.out.println(responseString); } } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码使用 HttpClient 发送了一个 GET 请求,并打印了响应内容。你可以根据需要修改代码以适应其他类型的请求和操作。 请注意,HttpClient 的最新版本是 HttpClient 5.x,但上述示例使用的是 HttpClient 4.x 的 API。你可以根据自己的需求选择使用哪个版本。 希望这个回答能对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值