JAVA的HttpResponse怎么实例化,org.apache.http.HttpResponse;
开发JAVA调用其他系统的接口,实例化HttpResponse是报错提示:CannotinstantiatethetypeHttpResponse,代码是抄别人网上介绍的...
开发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<String, String> headers, String encode) { HttpResponse response = new HttpResponse(); if (encode == null) { encode = "utf-8"; } String content = null; // since 4.3 不再使用 DefaultHttpClient CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url); // 设置header if (headers != null && headers.size() > 0) { for (Map.Entry<String, String> 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; }
展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询