java如何调用对方http接口
2018-06-07
展开全部
你是指发送http请求吗,可以使用Apache 的 HttpClient
//构建HttpClient实例
CloseableHttpClient httpclient = HttpClients.createDefault(); //设置请求超时时间
RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(60000) .setConnectTimeout(60000) .setConnectionRequestTimeout(60000) .build(); //指定POST请求
HttpPost httppost = new HttpPost(url);
httppost.setConfig(requestConfig); //包装请求体
List<NameValuePair> params = new ArrayList<NameValuePair>(); params.addAll(content);
HttpEntity request = new UrlEncodedFormEntity(params, "UTF-8"); //发送请求
httppost.setEntity(request);
CloseableHttpResponse httpResponse = httpclient.execute(httppost); //读取响应
HttpEntity entity = httpResponse.getEntity(); String result = null; if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询