java中用httpClient请求service,httpPost方法示例
2013-12-27 16:57
597 查看
所需jar包:
commons-logging-1.1.1.jar
commons-codec-1.4.jar
httpclient-4.1.2.jar
httpcore-4.1-alpha1.jar
/**
*
* 方法描述:
* @auth:danni
* @date:2013-12-27 下午04:25:07
* @param args
* @return:void
*/
public static void main(String[] args) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://test.service.cc");
// timestamp
String timestamp = System.currentTimeMillis() + "";
// 签名
String signature = SHA1.hex_sha1(secretkey + timestamp + username);//用sha1加密
HttpResponse response = null;
try {
JSONObject obj = new JSONObject();
obj.put("username", username);
obj.put("signature", signature);
obj.put("timestamp", timestamp);
System.out.println("TestHttpPost.main()" + obj.toString());
httppost.setEntity(new StringEntity(obj.toString()));
response = httpClient.execute(httppost);
int code = response.getStatusLine().getStatusCode();//成功请求
if (code == 200) {
String rev = EntityUtils.toString(response.getEntity());// 返回json格式:
// {"id":
// "27JpL~j4vsL0LX00E00005","version":
// "abc"}
System.out.println("TestHttpPost.main" + rev);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
commons-logging-1.1.1.jar
commons-codec-1.4.jar
httpclient-4.1.2.jar
httpcore-4.1-alpha1.jar
/**
*
* 方法描述:
* @auth:danni
* @date:2013-12-27 下午04:25:07
* @param args
* @return:void
*/
public static void main(String[] args) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://test.service.cc");
// timestamp
String timestamp = System.currentTimeMillis() + "";
// 签名
String signature = SHA1.hex_sha1(secretkey + timestamp + username);//用sha1加密
HttpResponse response = null;
try {
JSONObject obj = new JSONObject();
obj.put("username", username);
obj.put("signature", signature);
obj.put("timestamp", timestamp);
System.out.println("TestHttpPost.main()" + obj.toString());
httppost.setEntity(new StringEntity(obj.toString()));
response = httpClient.execute(httppost);
int code = response.getStatusLine().getStatusCode();//成功请求
if (code == 200) {
String rev = EntityUtils.toString(response.getEntity());// 返回json格式:
// {"id":
// "27JpL~j4vsL0LX00E00005","version":
// "abc"}
System.out.println("TestHttpPost.main" + rev);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
相关文章推荐
- java使用common-httpclient包实现post请求方法示例
- Java Web开发8__HTTP GET请求、POST请求的区别, 重写 service() 方法
- java中用httpClient请求service时bug解决方法(httpGet)
- 【java】 HttpClient发送POST请求示例
- java中用httpClient请求service时bug解决方法(httpGet)
- Java用org.apache.http.client的HttpClient发送Post请求 可获取返回Header
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- JAVA发送http get/post请求,调用http接口、方法
- Java用HttpClient3发送Get和Post请求(增强)
- HttpClient之Get请求和Post请求示例
- okHttpClient异步同步的get和post请求方法
- HttpClient get和post请求的示例代码以及防乱码处理
- Java HttpPost请求基于apache的httpclient
- Android HttpClient GET或者POST请求基本使用方法
- Java--org.apache.http.client的HttpClient发送Post请求,获取返回Header
- HttpClient之Get请求和Post请求示例
- Apache 大三方法HTTP请求链接响应之HttpClient的GET和POST工具类封装
- java apache commons HttpClient发送get和post请求的学习整理
- java http请求的get和post方法工具类
- [小代码]用HttpClient调POST请求方法