HttpClient发送http请求以及上传文件
2017-04-05 15:47
639 查看
1.封装请求体
封装普通请求体private UrlEncodedFormEntity getUrlEntry(Map<String, Object> param) throws UnsupportedEncodingException { List<NameValuePair> params = new ArrayList<NameValuePair>(); Iterator<String> iterator = param.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next(); params.add(new BasicNameValuePair(key, (String) param.get(key))); } UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(params, "UTF-8"); return uefEntity; }
封装上传文件请求体
private MultipartEntity getMutipartEntry(Map<String, Object> param, File file) throws UnsupportedEncodingException { if (file == null) { throw new IllegalArgumentException("文件不能为空"); } FileBody fileBody = new FileBody(file); FormBodyPart filePart = new FormBodyPart("file", fileBody); MultipartEntity multipartEntity = new MultipartEntity(); multipartEntity.addPart(filePart); Iterator<String> iterator = param.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next(); FormBodyPart field = new FormBodyPart(key, new StringBody((String) param.get(key))); multipartEntity.addPart(field); } return multipartEntity; }
2.发送请求
发送普通请求public String postUrl(String url,Map<String, Object> param) throws ClientProtocolException, IOException { String res = null; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(url); UrlEncodedFormEntity uefEntity = getUrlEntry(param); httppost.setEntity(uefEntity); CloseableHttpResponse response = httpClient.execute(httppost); HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { res = EntityUtils.toString(entity, "UTF-8"); response.close(); } else { res = EntityUtils.toString(entity, "UTF-8"); response.close(); throw new IllegalArgumentException(res); } return res; }
发送上传文件请求
public String postFile(String url,Map<String, Object> param, File file) throws ClientProtocolException, IOException { String res = null; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(url); httppost.setEntity(getMutipartEntry(param,file)); CloseableHttpResponse response = httpClient.execute(httppost); HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { res = EntityUtils.toString(entity, "UTF-8"); response.close(); } else { res = EntityUtils.toString(entity, "UTF-8"); response.close(); throw new IllegalArgumentException(res); } return res; }
相关文章推荐
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- java httpclient文件上传,springmvc作为服务器端接收文件,以及上传中文件名乱码的解决方法
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- HttpClient上传文件以及传输数据
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- HttpClient 4.5版本实现文件上传下载以及常用post、get请求
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- java模拟http请求上传文件,基于Apache的httpclient
- HttpClient请求接口上传文件以及参数
- HTML5 ( 借助http请求发送formdata对象,从而上传文件 ) XMLHttpRequest, FormData
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- 基于HttpClient访问服务器以及上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- HttpClient PostMethod模拟带文件上传+普通字段的http请求(可解决文件为网络文件的问题)
- 稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTcpBinding的支持, 在Socket通信中通过HTTP检索策略文件, HTTP请求中的ClientHttp和BrowserHttp