java httpresponse_JAVA的HttpResponse怎么实例化,org.apache.http.HttpResponse;

开发JAVA调用其他系统的接口,实例化HttpResponse是报错提示:CannotinstantiatethetypeHttpResponse,代码是抄别人网上介绍的,地址如下:https://www.cnblogs.com/ifindu-san/p/9277...

开发JAVA调用其他系统的接口,实例化HttpResponse 是报错提示:Cannot instantiate the type HttpResponse,代码是抄别人网上介绍的,地址如下:https://www.cnblogs.com/ifindu-san/p/9277967.html ,不知道是哪里出了问题,下载的jar如图;部分代码如下import java.io.File;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete;import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.HttpMultipartMode;import org.apache.http.entity.mime.MultipartEntityBuilder;import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; //import org.caeit.cloud.dev.entity.HttpResponse; // no usepublic class HttpClientUtil {/** * 发送http get请求 */public static HttpResponse httpGet(String url, Map headers, String encode) {HttpResponse response = new HttpResponse();if (encode == null) {encode = "utf-8";}String content = null;// since 4.3 不再使用 DefaultHttpClientCloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();HttpGet httpGet = new HttpGet(url);// 设置headerif (headers != null && headers.size() > 0) {for (Map.Entry entry : headers.entrySet()) {httpGet.setHeader(entry.getKey(), entry.getValue());}}CloseableHttpResponse httpResponse = null;try {httpResponse = closeableHttpClient.execute(httpGet);HttpEntity entity = httpResponse.getEntity();content = EntityUtils.toString(entity, encode);response.setBody(content);response.setHeaders(httpResponse.getAllHeaders());response.setReasonPhrase(httpResponse.getStatusLine().getReasonPhrase());response.setStatusCode(httpResponse.getStatusLine().getStatusCode());} catch (Exception e) {e.printStackTrace();} finally {try {httpResponse.close();} catch (IOException e) {e.printStackTrace();}}try { // 关闭连接、释放资源closeableHttpClient.close();} catch (IOException e) {e.printStackTrace();}return response;}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值