标签:代理 air 接收 成功 error: 发送数据包 and cti 主机
HTTP协议在互联网无处不在,在前面的章节中记录了‘接口本质即协议’,因此,接口测试首先了解的便是协议,其发送数据包和接收数据包的过程,其次便是如何在测试中去发送去解析,不论是通过代码还是工具也好,抽空学习一下HttpClient。
(官方地址:http://hc.apache.org/httpclient-3.x/)
HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。(百度百科)
HttpClient于2001年作为Jakarta Commons的子项目启动,基于Jakarta Slide项目开发的代码 。它于2004年从下议院晋升,毕业于另一个雅加达项目。2005年,雅加达的HttpComponents项目被创建,其任务是开发HttpClient 3.x的后继产品,并维护现有的代码库,直到新的代码库准备好接管。该共享项目,摇篮HttpClient的,离开雅加达 ,2007年成为一个独立的顶级项目。同年晚些时候, HttpComponents 项目还让雅加达成为一个独立的顶级项目,负责维护HttpClient。
它的一个基本特征还是有必要去了解的:
百度百科上有其详细的执行步骤:
1、创建Httpclient实例;
2、创建某种连接方法的实例,在这里是GetMethod。在 GetMethod 的构造函数中传入待连接的地址;
3、调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例;
4、读 response;
5、释放连接。无论执行方法是否成功,都必须释放连接;
6、对得到后的内容进行处理。
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("http://www.apache.org/");
// set per default client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
byte [] responseBody = method.getResponseBody();
method.releaseConnection();
System.out.println(new String(responseBody));
import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; import org.apache.commons.httpclient.params.HttpMethodParams; import java.io.*; public class HttpClientTutorial { private static String url = "http://www.apache.org/"; public static void main(String[] args) { // Create an instance of HttpClient. HttpClient client = new HttpClient(); // Create a method instance. GetMethod method = new GetMethod(url); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data System.out.println(new String(responseBody)); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } } }
该例使用的是Get方法,那么我们日常中使用的方法最多的无外乎Get和Post方法,那么就找一下这两个方法的例子。
该实例取自:https://www.cnblogs.com/sharpest/p/7831350.html。
/** * @author Richered * HttpClientGET */ package com.me.learn; 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.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class HttpClientGET { public static void main(String []args) { //创建httpclient对象 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //创建httpclientget方法对象 HttpGet httpGet = new HttpGet("https://www.baidu.com"); CloseableHttpResponse response = null; try { // 3. 执行GET请求 response = httpClient.execute(httpGet); System.out.println(response.getStatusLine()); // 4. 获取响应实体 HttpEntity entity = response.getEntity(); // 5. 处理响应实体 if (entity != null) { System.out.println("长度:" + entity.getContentLength()); System.out.println("内容:" + EntityUtils.toString(entity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 6. 释放资源 try { response.close(); httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * @author Richered * HttpClientPOST */ package com.me.learn; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class HttpClientPOST { public static void main(String[] args) { // TODO Auto-generated method stub // 1. 创建HttpClient对象 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 2. 创建HttpPost对象 HttpPost post = new HttpPost( "https://www.baidu.com"); // 3. 设置POST请求传递参数 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", "test")); params.add(new BasicNameValuePair("password", "12356")); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params); post.setEntity(entity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // 4. 执行请求并处理响应 try { CloseableHttpResponse response = httpClient.execute(post); HttpEntity entity = response.getEntity(); if (entity != null){ System.out.println("响应内容:"); System.out.println(EntityUtils.toString(entity)); } response.close(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 释放资源 try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } }
标签:代理 air 接收 成功 error: 发送数据包 and cti 主机
原文地址:https://www.cnblogs.com/richered/p/9867892.html