importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.nio.charset.Charset;importjava.util.Base64;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.HttpStatus;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.util.EntityUtils;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.lmbx.util.DES;importcom.lmbx.util.DESCoderUtil;importlombok.extern.slf4j.Slf4j;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4jpublic classtest2 {
@Testpublic void test() throwsException {
String url="http://localhost:8080/v1/api/test";
String json="{\"placeId\":\"2\",\"mediaCode\":\"ottauto\",\"cId\":\"1\"}";
String responseBodyMw=new String(Base64.getDecoder().decode(json), "utf-8");
String doPost= doPost(url,responseBodyMw,null);
System.out.println(doPost);
}public synchronized staticJSONObject doGet(String url) {
JSONObject jsonObject=null;try{
HttpClient client= newDefaultHttpClient();
HttpGet request= newHttpGet(url);
System.out.println(request);
HttpResponse response=client.execute(request);if (response.getStatusLine().getStatusCode() ==HttpStatus.SC_OK) {
String strResult=EntityUtils.toString(response.getEntity());
System.out.println("请求结果为——》"+strResult);
jsonObject=JSON.parseObject(strResult);returnjsonObject;
}
}catch(IOException e) {
e.printStackTrace();
}returnjsonObject;
}public staticString doPost(String url,String json,String token){
DefaultHttpClient client= newDefaultHttpClient();
HttpPost post= newHttpPost(url);
String response= null;try{
StringEntity s= new StringEntity(json,Charset.forName("UTF-8"));
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
post.setHeader("token", token);
post.setHeader("Content-Type", "application/json; charset=UTF-8");
HttpResponse res=client.execute(post);if(res.getStatusLine().getStatusCode() ==HttpStatus.SC_OK){
HttpEntity httpEntity=res.getEntity();
BufferedReader reader= null;
reader= new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"), 10 * 1024);
StringBuilder strBuilder= newStringBuilder();
String line= null;while ((line = reader.readLine()) != null) {
strBuilder.append(line);
}
response=strBuilder.toString();
}
}catch(Exception e) {throw newRuntimeException(e);
}returnresponse;
}
}