淘淘商城17_httpclient的使用01

1.什么是httpclient

HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
下载地址:

http://hc.apache.org/

2. httpclient使用方式

发送请求的方式,get和post两种.

其实httpClient就是模拟的一个浏览器,去请求数据

3. 编写一个小例子(如何获取https://www.sogou.com/这个浏览器的数据)

httpclient同jsonp,webservice都是可以解决跨域的问题

例子:如何获取https://www.sogou.com/这个浏览器的数据

这里写一个测试类

3.1. 添加httpclient的jar包

在taotao-portal的pom.xml里添加

<!-- httpclient -->
			<dependency>
				<groupId>org.apache.httpcomponents</groupId>
				<artifactId>httpclient</artifactId>
			</dependency>
			
			<!-- 单元测试 -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<scope>test</scope>
			</dependency>
	</dependencies>

3.2. 测试类

3.2.1 不带参数的get请求

package com.taotao.test;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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;
import org.junit.Test;

public class HttpClientTest {

	@Test
	public void doGet() throws Exception {
		//创建一个httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//创建一个GET对象
		HttpGet get = new HttpGet("http://www.sogou.com");
		//执行请求
		CloseableHttpResponse response = httpClient.execute(get);
		//取响应的结果
		int statusCode = response.getStatusLine().getStatusCode();
		System.out.println(statusCode);
		HttpEntity entity = response.getEntity();
		String string = EntityUtils.toString(entity, "utf-8");
		System.out.println(string);
		//关闭httpclient
		response.close();
		httpClient.close();
	}
}

 测试结果

 

修改一下url:

测试结果:

3.2.2 带参数的get请求 

/**
	 * 带参数的get请求
	 * @throws Exception
	 */
	@Test
	public void doGetWithParam() throws Exception{
		//创建一个httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//创建一个uri对象
		URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
		uriBuilder.addParameter("query", "老九门");
		HttpGet get = new HttpGet(uriBuilder.build());
		//执行请求
		CloseableHttpResponse response = httpClient.execute(get);
		//取响应的结果
		int statusCode = response.getStatusLine().getStatusCode();
		System.out.println(statusCode);
		HttpEntity entity = response.getEntity();
		String string = EntityUtils.toString(entity, "utf-8");
		System.out.println(string);
		//关闭httpclient
		response.close();
		httpClient.close();
	}

 3.2.3 post请求,不带参数

/**
	 * httpclient的post请求,不带参数
	 * @return
	 */
	@RequestMapping(value = "/httpclient/post", method = RequestMethod.POST)
	@ResponseBody
	public TaotaoResult getPost() {
		return TaotaoResult.ok();
	}
/**
	 * httpclient的post请求,不带参数
	 * @throws Exception
	 */
	@Test
	public void doPost() throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
	
		//创建一个post对象
		HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");
		//执行post请求
		CloseableHttpResponse response = httpClient.execute(post);
		String string = EntityUtils.toString(response.getEntity());
		System.out.println(string);
		response.close();
		httpClient.close();
		
	}

 测试返回结果:

在桌面上创建一个TXT文档,改为111.html格式。

然后将上图报错的这一行复制到111.html里面,保存

然后以浏览器的方式打开即可知道什么错误

 

修改错误

将taotao-portal里面的web.xml修改

<servlet-mapping>
		<servlet-name>taotao-portal</servlet-name>
		<!-- 伪HTML页面 -->
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>	
	<servlet-mapping>
		<servlet-name>taotao-portal</servlet-name>
		<!-- action页面 -->
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>	

 

 

最后运行taotao-portal,测试即可

3.2.4 post请求 ,带参数

 IndexController.java

/**
	 * 带参数httpclient的post请求
	 * @param username
	 * @param pwd
	 * @return
	 */
	@RequestMapping(value = "/httpclient/post", method = RequestMethod.POST)
	@ResponseBody
	public String getPost(String username, String pwd) {
		return "username=" + username + "\tpwd=" + pwd;
	}

HttpClientTest.java

/**
	 * 带参数的post请求
	 * @throws Exception
	 */
	@Test
	public void doPostWithParam() throws Exception{
		CloseableHttpClient httpClient = HttpClients.createDefault();
		
		//创建一个post对象
		HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.action");
		//创建一个Entity。模拟一个表单
		List<NameValuePair> kvList = new ArrayList<>();
		kvList.add(new BasicNameValuePair("username", "zhangsan"));
		kvList.add(new BasicNameValuePair("pwd", "123"));
		
		//包装成一个Entity对象
		StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");
		//设置请求的内容
		post.setEntity(entity);
		
		//执行post请求
		CloseableHttpResponse response = httpClient.execute(post);
		String string = EntityUtils.toString(response.getEntity());
		System.out.println(string);
		response.close();
		httpClient.close();
	}

 测试结果

问题: 如果将username的值改为中文的话(张三),会乱码

问题处理:解决Post请求乱码问题

使用post的提交,传递中文会出现乱码问题,是在返回客户端出现的乱码

	/**
	 * 带参数httpclient的post请求
	 * @param username
	 * @param pwd
	 * @return
	 */
	@RequestMapping(value = "/httpclient/post", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE
			+ ";charset=utf-8")
	@ResponseBody
	public String getPost(String username, String pwd) {
        System.out.println(username);
		return "username=" + username + "\tpwd=" + pwd;
	}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值