如何使用HttpClient
2个回答
2016-03-04 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
httpClient使用举例:
HttpClient client = new HttpClient();
String url = ana/workFlowOaInterface.do?action=oaTask;
PostMethod postMethod = new PostMethod(url);
//设置参数编码为gbk
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gbk");
//构造键值对参数
NameValuePair[] data = { new NameValuePair("processInstanceId", "230"), new NameValuePair("approveFlag", "1") };
// 把参数值放入postMethod中
postMethod.setRequestBody(data);
//执行
client.executeMethod(postMethod);
//读取内容
byte[] responseBody = postMethod.getResponseBody();
//处理内容
System.out.println(new String(responseBody));
System.out.println("getStatusLine:"+postMethod.getStatusLine());
System.out.println("~~~"+postMethod.getResponseBodyAsString());
System.out.println("statusCode:"+statusCode);
//打印结果页面
String response = new String(postMethod.getResponseBodyAsString().getBytes("utf-8"));
//打印返回的信息
System.out.println("response:"+response);
//释放连接
postMethod.releaseConnection();
如果传递的是中文参数,有可能会出现乱码,通过设置正确的参数编码来解决。
在被调用的接口方,直接通过request.getParameter的方式获取。
HttpClient client = new HttpClient();
String url = ana/workFlowOaInterface.do?action=oaTask;
PostMethod postMethod = new PostMethod(url);
//设置参数编码为gbk
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gbk");
//构造键值对参数
NameValuePair[] data = { new NameValuePair("processInstanceId", "230"), new NameValuePair("approveFlag", "1") };
// 把参数值放入postMethod中
postMethod.setRequestBody(data);
//执行
client.executeMethod(postMethod);
//读取内容
byte[] responseBody = postMethod.getResponseBody();
//处理内容
System.out.println(new String(responseBody));
System.out.println("getStatusLine:"+postMethod.getStatusLine());
System.out.println("~~~"+postMethod.getResponseBodyAsString());
System.out.println("statusCode:"+statusCode);
//打印结果页面
String response = new String(postMethod.getResponseBodyAsString().getBytes("utf-8"));
//打印返回的信息
System.out.println("response:"+response);
//释放连接
postMethod.releaseConnection();
如果传递的是中文参数,有可能会出现乱码,通过设置正确的参数编码来解决。
在被调用的接口方,直接通过request.getParameter的方式获取。
展开全部
GET 方式
Java代码
//先将参数放入List,再对参数进行URL编码
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "中国"));
params.add(new BasicNameValuePair("param2", "value2"));
//对参数编码
String param = URLEncodedUtils.format(params, "UTF-8");
//baseUrl
String baseUrl = "http://ubs.free4lab.com/php/method.php";
//将URL与参数拼接
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //发起GET请求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
POST方式
Java代码
//和GET方式一样,先将参数放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));
params.add(new BasicNameValuePair("param2", "第二个参数"));
try {
HttpPost postMethod = new HttpPost(baseUrl);
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //执行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Java代码
//先将参数放入List,再对参数进行URL编码
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "中国"));
params.add(new BasicNameValuePair("param2", "value2"));
//对参数编码
String param = URLEncodedUtils.format(params, "UTF-8");
//baseUrl
String baseUrl = "http://ubs.free4lab.com/php/method.php";
//将URL与参数拼接
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //发起GET请求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
POST方式
Java代码
//和GET方式一样,先将参数放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));
params.add(new BasicNameValuePair("param2", "第二个参数"));
try {
HttpPost postMethod = new HttpPost(baseUrl);
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //执行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询