westful httpclient dopost调用接口
来源:互联网 发布:网络找不到打印机 编辑:程序博客网 时间:2024/04/29 18:37
<textarea readonly="readonly" name="code" class="java">
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;public class HttpClientUtil {
//
public static final String learnSelUrl="http://192.168.0.124:8080/site/trdcredit/jxlxxwactivereq";
/**
* post请求
* @param url
* @param param
* @return
* @throws IOException
*/
public static JSONObject postJsonData(String url,Map<String,String> params){
CloseableHttpClient httpclient = HttpClientUtil.createDefault();
HttpPost httpPost = new HttpPost(url);
//拼接参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
NameValuePair pair = new BasicNameValuePair(key, value);
list.add(pair);
}
CloseableHttpResponse response=null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(list));
response = httpclient.execute(httpPost);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**请求发送成功,并得到响应**/
JSONObject jsonObject=null;
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity httpEntity = response.getEntity();
String result=null;
try {
result = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}// 返回json格式:
jsonObject = JSONObject.fromObject(result);
}
return jsonObject;
}
/**
* Creates {@link CloseableHttpClient} instance with default
* configuration.
*/
public static CloseableHttpClient createDefault() {
return HttpClientBuilder.create().build();
}
}
//方法二
CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(”调用地址“); //拼接参数 List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("参数队列头部", 调用参数)); System.out.println("==== 提交参数 ======" +list); CloseableHttpResponse response = null; try { httpPost.setEntity(new UrlEncodedFormEntity(list)); response = httpClient.execute(httpPost); System.out.println("========HttpResponseProxy:========"+response.getStatusLine()); HttpEntity entity = response.getEntity(); if(entity != null){ String result = EntityUtils.toString(entity, "UTF-8"); System.out.println("========接口返回=======" +result); } EntityUtils.consume(entity); //httpClient.getConnectionManager().shutdown(); } catch (Exception e) { e.printStackTrace(); }finally { if(response != null){ try { response.close(); } catch (IOException e) { e.printStackTrace(); } } if(httpClient != null){ try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } }
//关闭参数流操作
public static void consume(final HttpEntity entity) throws IOException { if (entity == null) { return; } if (entity.isStreaming()) { final InputStream instream = entity.getContent(); if (instream != null) { instream.close(); } } }
</textarea>
阅读全文
0 0
- westful httpclient dopost调用接口
- HttpClient doPost
- HttpClient调用接口
- httpclient接口调用
- httpclient http接口调用
- httpClient 接口调用
- httpClient接口调用
- java使用httpClient调用接口
- httpClient入门到精通-------doPost
- 使用httpclient实现http接口调用实例
- 使用httpclient实现http接口调用实例
- 使用 httpClient 调用新浪微博接口
- java HttpClient get post请求 调用接口
- 使用httpclient实现http接口调用实例
- spring集成httpclient调用远端接口
- 关于HttpClient(调用openstack rest接口失败)
- httpClient发送get请求调用接口
- 使用httpclient实现http接口调用实例
- flush , shutdownOutput, close有什么区别
- spark2
- 位运算---在其他数都出现k次的数组中找到只出现一次的数
- 开发一流Android SDK
- CrodeForces 851A--#432 Div2 A
- westful httpclient dopost调用接口
- 简单说 通过JS控制CSS的各种方式(上)
- CrodeForces 850A--#432 Div2 C
- WebService 应用场景
- web缓存
- 服务器缓存
- switch所支持的参数类型
- Vue入门学习-使用服务器传来的JSON数据交给Vue渲染HTML页面
- android 由模块化到组件化(一)