http get/post请求及json解析
2013-03-31 16:41
411 查看
public static String httpRequest(String url, String jsonString, String codeType) {
StringBuffer result = new StringBuffer();
log.debug("http请求:" + url);
org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpPost post = new HttpPost(url);
StringEntity s;
HttpResponse res = null;
try {
if(StringUtils.isNotEmpty(jsonString)) {
log.debug("post请求串:" + jsonString);
s = new StringEntity(jsonString);
s.setContentEncoding(codeType);
s.setContentType("application/json");
post.setEntity(s);
res = httpClient.execute(post);
} else {
res = httpClient.execute(get);
}
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity();
JSONTokener tokener = new JSONTokener(new InputStreamReader(entity.getContent(), codeType));
char c = '\0';
while ((c = tokener.next()) != '\0') {
result.append(c);
}
log.debug("http响应:" + result.toString());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (org.json.JSONException e) {
e.printStackTrace();
}
return result.toString();
jar包:json-lib-2.4,commons-httpClient-3.1
StringBuffer result = new StringBuffer();
log.debug("http请求:" + url);
org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpPost post = new HttpPost(url);
StringEntity s;
HttpResponse res = null;
try {
if(StringUtils.isNotEmpty(jsonString)) {
log.debug("post请求串:" + jsonString);
s = new StringEntity(jsonString);
s.setContentEncoding(codeType);
s.setContentType("application/json");
post.setEntity(s);
res = httpClient.execute(post);
} else {
res = httpClient.execute(get);
}
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity();
JSONTokener tokener = new JSONTokener(new InputStreamReader(entity.getContent(), codeType));
char c = '\0';
while ((c = tokener.next()) != '\0') {
result.append(c);
}
log.debug("http响应:" + result.toString());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (org.json.JSONException e) {
e.printStackTrace();
}
return result.toString();
jar包:json-lib-2.4,commons-httpClient-3.1
相关文章推荐
- python http请求post参数,json解析
- Netty学习笔记之一(Netty解析简单的Http Post Json 请求)
- 使用apache的httpclient发送post请求传送和解析json
- Java HttpConnection Post Json 请求接口修改密码,接收返回值
- Http连接GET/POST请求
- HttpPost 传输Json数据并解析
- Android—Http连接之GET/POST请求
- java socket解析http消息 GET/POST 携带单个附件
- java实现HTTP的post请求 key-value格式与json格式的区别
- Java http get/post 传递json参数问题
- java http 请求之post json数据
- C# HttpWebRequest\HttpWebResponse\WebClient发送请求解析json数据
- JAVA HTTP POST请求(请求参数格式为json)
- 关于java 发送http json数据格式请求时,服务器端如何接收json数据并解析
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- Apache http 发送post请求,解析响应
- 解析HTTP协议六种请求方法,get,head,put,delete,post有什么区别
- C++ 实现 发送HTTP Get/Post请求 good
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- C++ 实现 发送HTTP Get/Post请求