使用Apache HTTP发送POST/GET请求
来源:互联网 发布:淘宝智能版多少钱 编辑:程序博客网 时间:2024/06/05 20:30
POST请求
方法一,直接传入Instream
public static String post(String url, InputStream instream){ try { //该Request是org.apache.http.client.fluent.Request; HttpEntity entity = Request.Post(url) .bodyStream(instream,ContentType.create("text/html", "UTF-8")) .execute().returnResponse().getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } catch (Exception e) { logger.error("post请求异常," + e.getMessage() + "\n post url:" + url); e.printStackTrace(); } return null; }
方法二,传入普通json字符串
private static String httpPost(String url, String data) { try { HttpEntity entity = Request.Post(url) .bodyString(data,ContentType.create("text/html", "UTF_8")) .execute().returnResponse().getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } catch (Exception e) { logger.error("post请求异常," + e.getMessage() + "\n post url:" + url); e.printStackTrace(); } return null; }
GET请求
private static String httpGet(String url) { try { HttpEntity entity = Request.Get(url). execute().returnResponse().getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } catch (Exception e) { logger.error("get请求异常," + e.getMessage() + "\n get url:" + url); e.printStackTrace(); } return null; }
文件上传
public static String postFile(String url,String name,File file){ try { HttpEntity reqEntity = MultipartEntityBuilder.create().addBinaryBody(name, file).build(); Request request = Request.Post(url); request.body(reqEntity); HttpEntity resEntity = request.execute().returnResponse().getEntity(); return resEntity != null ? EntityUtils.toString(resEntity) : null; } catch (Exception e) { logger.error("postFile请求异常," + e.getMessage() + "\n post url:" + url); e.printStackTrace(); } return null; }
0 0
- 使用Apache HTTP发送POST/GET请求
- 使用Apache HttpClient发送get、post请求
- 使用socket发送http请求(GET/POST)
- 使用socket发送http请求(GET/POST)
- 使用socket发送http请求(GET/POST)
- HTTP 发送get post请求
- http发送get、post请求
- Http发送GET/POST请求
- Android HTTP实例 使用GET方法和POST方法发送请求(通过Apache接口实现)
- 使用Java发送HTTP发送POST、GET请求
- Android使用Apache HttpClient发送GET、POST请求
- 使用Apache HttpClient4.x 发送 GET POST 请求
- JAVA 发送Http请求,GET、POST请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Quartz定时器的使用
- hibernate EntityNotFoundException异常处理
- Restful风格API中用put还是post做新增操作有什么区别?
- 2014.4新版uboot启动流程分析
- PNP型三极管____本人备注
- 使用Apache HTTP发送POST/GET请求
- Spring学习笔记-1:SpringMVC hello world
- android 轮播图
- 设计模式-观察者模式
- Determining IP information for eth0...failed
- poj2777 Count Color 线段树区间更新+位操作
- Splash.activity闪屏页面的设置
- SDKD 2016 Summer Single Contest #02.A
- Android实战技巧之十二:Android Studio导入第三方类库、jar包和so库