Java发送Http请求

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Java发送Http请求1、⽅法⼀,通过apache的httpclient

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.apache.http.HttpEntity;

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.ContentType;

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.protocol.HTTP;

import org.apache.http.util.EntityUtils;

import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class HttpTest {

String uri = "http://127.0.0.1:8080/simpleweb";

/**

* Get⽅法

*/

@Test

public void test1() {

try {

CloseableHttpClient client = null;

CloseableHttpResponse response = null;

try {

HttpGet httpGet = new HttpGet(uri + "/test1?code=001&name=测试");

client = HttpClients.createDefault();

response = client.execute(httpGet);

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity);

System.out.println(result);

} finally {

if (response != null) {

response.close();

}

if (client != null) {

client.close();

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Post发送form表单数据

*/

@Test

public void test2() {

try {

CloseableHttpClient client = null;

CloseableHttpResponse response = null;

try {

// 创建⼀个提交数据的容器

List<BasicNameValuePair> parames = new ArrayList<>();

parames.add(new BasicNameValuePair("code", "001"));

parames.add(new BasicNameValuePair("name", "测试"));

HttpPost httpPost = new HttpPost(uri + "/test1");

httpPost.setEntity(new UrlEncodedFormEntity(parames, "UTF-8"));

client = HttpClients.createDefault();

response = client.execute(httpPost);

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity);

System.out.println(result);

} finally {

if (response != null) {

response.close();

if (client != null) {

client.close();

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Post发送json数据

*/

@Test

public void test3() {

try {

CloseableHttpClient client = null;

CloseableHttpResponse response = null;

try {

ObjectMapper objectMapper = new ObjectMapper();

Map<String, Object> data = new HashMap<String, Object>();

data.put("code", "001");

data.put("name", "测试");

HttpPost httpPost = new HttpPost(uri + "/test2");

httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");

httpPost.setEntity(new StringEntity(objectMapper.writeValueAsString(data),

ContentType.create("text/json", "UTF-8")));

client = HttpClients.createDefault();

response = client.execute(httpPost);

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity);

System.out.println(result);

} finally {

if (response != null) {

response.close();

}

if (client != null) {

client.close();

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

2、⽅法⼆,通过JDK⾃带的HttpURLConnection

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import .HttpURLConnection;

import .URL;

import java.util.HashMap;

import java.util.Map;

import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class HttpApi {

String uri = "http://127.0.0.1:7080/simpleweb";

/**

* Get⽅法

*/

@Test

public void test1() {

try {

URL url = new URL(uri + "/test1?code=001&name=测试");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setDoOutput(true); // 设置该连接是可以输出的

connection.setRequestMethod("GET"); // 设置请求⽅式

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); String line = null;

StringBuilder result = new StringBuilder();

while ((line = br.readLine()) != null) { // 读取数据

result.append(line + "\n");

相关文档
最新文档