您的位置:首页 > 理论基础 > 计算机网络

【接口测试】:HttpClient Post 和 Get 请求

2017-08-22 16:06 756 查看
On Thy Way



package com.Study.Demo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class httpClientDeom {

//httpclient的get()方法
public void get() {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建httpget.
HttpGet httpget = new HttpGet("https://www.baidu.com/");
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容长度
System.out.println("Response content length: " + entity.getContentLength());
// 打印响应内容
System.out.println("Response content: " + EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void post(){
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost("http://api.altamob.com/adserver/v1/promote/ads/sdk/v3");
//添加header
httppost.addHeader("token", "170da1dd-d2b9-5d9d-0a5b-c6bf24d401f7");
JSONObject params = new JSONObject();
params.put("app_pkg", "com.quvideo.xiaoying");
params.put("app_version", "1.04");
params.put("gaid", "d3d8b9cb-72de-424d-871d-c1d19d1e8016");
params.put("aid", "6661bb5690d8a34d");
params.put("os_version", "6.0.1");
params.put("user_agent", "Dalvik2.1.0");
params.put("sdk_version", "3.4.1.1");
params.put("language", "en");
params.put("network_type", "1");
JSONObject response = null;
try {
StringEntity s = new StringEntity(params.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
httppost.setEntity(s);
HttpResponse res = httpclient.execute(httppost);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
System.out.println("result : "+result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public static void main(String[] args){
new httpClientDeom().get();
System.out.println("------------post-------");
new httpClientDeom().post();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  接口 HttpClient