HttpServiceTest.java
3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.ecar.apm;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
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.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.alibaba.fastjson.JSON;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HttpServiceTest {
@Test
public void testGet() throws ClientProtocolException, IOException, URISyntaxException{
CloseableHttpClient httpclient = HttpClients.createDefault();
URIBuilder builder = new URIBuilder("http://localhost:8080/httpService/sendGetData?name=jection11");
//builder.setParameter("name", "jection");
builder.setParameter("city", "shenzheng11");
HttpGet httpGet = new HttpGet(builder.build());
CloseableHttpResponse response2 = httpclient.execute(httpGet);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
System.out.println(EntityUtils.toString(entity2));
} finally {
response2.close();
}
}
//@Test
public void test() throws ClientProtocolException, IOException{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/httpService/sendPostDataByJson");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("name", "jection"));
nvps.add(new BasicNameValuePair("city", "shenzheng"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
System.out.println(EntityUtils.toString(entity2));
} finally {
response2.close();
}
}
//@Test
public void test1() throws ClientProtocolException, IOException{
Map<String, String> map = new HashMap<String, String>();
map.put("name", "wyj");
map.put("city", "南京");
String json = JSON.toJSONString(map);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/httpService/sendPostDataByJson");
// 设置参数到请求对象中
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
stringEntity.setContentEncoding("utf-8");
httpPost.setEntity(stringEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
}
response.close();
}
}