packagecom.ytinf.service.lrTest;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.NameValuePair;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.utils.HttpClientUtils;importorg.apache.http.client.utils.URIBuilder;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;public classGetRequest {private Listparams;publicGetRequest() {}public GetRequest(Listparams) {this.params =params;
}public void setParams(Listparams) {this.params =params;
}//直接传入要发送的参数列表
public JSONObject get(String url, List params) throwsException {
CloseableHttpClient httpClient=HttpClients.createDefault();
URIBuilder uriBuilder=newURIBuilder(url);
uriBuilder.setParameters(params);
HttpGet httpGet=newHttpGet(uriBuilder.build());
CloseableHttpResponse response=null;
String result=null;try{
response=httpClient.execute(httpGet);//System.out.println(response.getStatusLine().getStatusCode());
HttpEntity httpEntity=response.getEntity();//System.out.println(EntityUtils.toString(httpEntity));
result=EntityUtils.toString(httpEntity);
}finally{if (response!=null){
response.close();
}
httpClient.close();
}returnJSONObject.parseObject(result);
}//可以直接 get URL,也可以通过单个add()方法,添加参数后 再执行
public JSONObject get(String url) throwsException {
CloseableHttpClient httpClient=HttpClients.createDefault();
URIBuilder uriBuilder=newURIBuilder(url);if (this.params!=null){
uriBuilder.setParameters(this.params);
}
HttpGet httpGet=newHttpGet(uriBuilder.build());
CloseableHttpResponse response=null;
String result=null;try{
response=httpClient.execute(httpGet);//System.out.println(response.getStatusLine().getStatusCode());
HttpEntity httpEntity=response.getEntity();//System.out.println(EntityUtils.toString(httpEntity));
result=EntityUtils.toString(httpEntity);
}finally{if (response!=null){
response.close();
}
httpClient.close();
}returnJSONObject.parseObject(result);
}//给实例变量添加 单个参数
public voidremoveAllParameters(){if (this.params!=null && params.size()>0){this.params.clear();
}
}//给实例变量添加 单个参数
public voidaddParameter(String name,String value){if (this.params==null){this.params=new ArrayList<>();
}this.params.add(newBasicNameValuePair(name,value));
}public static void main(String[] args) throwsException{
String url="http://XX.XX.XX.XX:8089/databridge_dg/hbase/lottery";
GetRequest getRequest=newGetRequest();
List nameValuePairList=new ArrayList<>();
nameValuePairList.add(new BasicNameValuePair("lotterysn","910440142860045587600108"));
System.out.println(getRequest.get(url,nameValuePairList));
getRequest.addParameter("lotterysn","910540078760041513600403");
JSONObject jsonObject=getRequest.get(url);if (jsonObject.get("status").equals(0)){
System.out.println("成功!");
}else{
System.out.println("失败!");
}
url="http://XX.XX.XX.XX:8089/databridge_dg/hbase/win_lottery";
getRequest.removeAllParameters();
getRequest.addParameter("drawidlotterysn","175660110610175660092646500708");
JSONObject jsonObject1=getRequest.get(url);
System.out.println("中奖票查询的响应结果:"+jsonObject1);
}
}