发送Json请求,结果返回Json.
public static JSONObject post(String url,JSONObject json){
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
HttpEntity entity = res.getEntity();
String charset = EntityUtils.getContentCharSet(entity);
response = new JSONObject(new JSONTokener(new InputStreamReader(entity.getContent(),charset)));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
分享到:
相关推荐
NULL 博文链接:https://cxl2012.iteye.com/blog/2210521
NULL 博文链接:https://javasam.iteye.com/blog/2117845
本方法以HttpClient发送请求,并且接收返回数据 举例说明 public static String doGet(String url, Map, String> params, String charset) public static void downLoadImage(String url, String path) public ...
httpclient get/post请求工具类(map参数封装),方便调用
花了大半天写的测试类,用的时候记得改下引用的包名,可以当util工具类用,其他说明见博客 https://blog.csdn.net/akxj2022/article/details/88691698 流方式发送接收: ...觉得可以的话,又不想复制粘贴,就花点积分...
利用httpclient封装了http的POST和GET请求方式,封装方法多样,返回结果为json
HttpClient的工具类,用于发送get和post请求的工具类,数据类型可以使XML,JSON
httpClient的get请求方式2 * @return * @throws Exception */ public static String doGet(String url, String charset) throws Exception { /* * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:...
本功能支持文件上传 下载,支持请求流,支持JSON、XML请求,本版本是httpclient 4,其它jar包自行下载
一款基于HttpClient的工具类。 可二次开发 ,目前已集成文件上传 参数传递 Json传输 请求头添加等功能
多年积累,功能比较强大,可设置路由连接数,时间,请求类型包括get,post, 参数包括urlcode,map,json,xml。注释很清楚。
这是一个用于http请求的工具类,依赖jar包,org.apache.httpcomponents.httpClient,调用此方法,可以方便的就像http的调用,要求返回String(json)数据
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
java http工具,包括get、post、json格式请求,使用httpclient
根据传入的对象类型(支持list),参数,请求URL地址,利用httpclient进行远程接口调用返回,并自动封装返回对象。简单调用示例如下: new AsyncTask, AppException, Void>() { @Override protected Void ...
这三个工具类是自己开发中编写的,非常实用 其中HttpClientUtils包含代理设置, postJson 是通过代理post请求 postJsonNotProxy 不通过代理post请求
所用知识:http网络请求(url urlConnection HttpURLConnection HttpClient)、 volley、 MediaPlay、 video、webView、Json解析 等 功能:个人 http 简单的网路请求测试工具。 测试 url 网络资源请求获取 url.open...
http工具类,用户java后台请求其他接口,有post请求方式,get请求方式,正常情况下String JSON_CONTENT_TYPE="application/json; encoding=utf-8";