HttpClient使用Post和Get提交参数
来源:互联网 发布:网络热议话题 编辑:程序博客网 时间:2024/05/17 06:34
package httpclient;import java.io.IOException;import java.net.URLEncoder;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;public class HttpClientTest {public static void main(String[] args) throws Exception{String url = "/webservices/DomesticAirline.asmx/getDomesticAirlinesTime";String host = "www.webxml.com.cn";String param = "startCity="+URLEncoder.encode("杭州", "utf-8")+"&lastCity=&theDate=&userID=";HttpClient httpClient = new HttpClient();httpClient.getHostConfiguration().setHost(host, 80, "http");HttpMethod method = getMethod(url, param);//HttpMethod method = postMethod(url);httpClient.executeMethod(method);String response = method.getResponseBodyAsString();//String response = new String(method.getResponseBodyAsString().getBytes("ISO-8859-1"));System.out.println(response);}private static HttpMethod getMethod(String url,String param) throws IOException{GetMethod get = new GetMethod(url+"?"+param);get.releaseConnection();return get;}private static HttpMethod postMethod(String url) throws IOException{ PostMethod post = new PostMethod(url);post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk"); NameValuePair[] param = { new NameValuePair("startCity","杭州"),new NameValuePair("lastCity","沈阳"),new NameValuePair("userID",""),new NameValuePair("theDate","")} ; post.setRequestBody(param); post.releaseConnection();return post;}}
如果PostMethod提交的是中文字符,需要加上相应的编码格式:
post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");
如果GetMethod提交的参数有中文字符,需要先转换成utf-8格式:
URLEncoder.encode("杭州", "utf-8");
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- HttpClient用Post和Get带参数提交帮助类
- HttpClient使用get、post提交请求
- httpclient使用post提交json参数
- HttpClient post 和get 带参数请求
- Android通过Get,Post,HttpClient方式提交参数给服务器
- Android通过Get,Post,HttpClient方式提交参数给服务器
- Post和Get在HttpClient的使用
- 使用HttpClient进行get和post请求
- 使用HttpClient发送GET和POST请求
- HttpClient get, post使用
- Android-使用HttpClient框架提交数据(get/post)
- String、StringBuffer与StringBuilder之间区别
- Myeclipse 8.5 配置SVN
- [C++]字节对齐与结构体大小
- UNIX 高手的 20 个习惯 .
- 如何从Internet上有效而稳定地下载文件
- HttpClient使用Post和Get提交参数
- roosephu 考题之二: 进制法贪心
- Nihao Flash3D v1.0稳定版发布
- Android JNI 使用的数据结构JNINativeMethod详解
- 取得某列中第n大的行 使用dense_rank()函数
- 按home键程序退出时调用的方法
- PHP下得到客户端ip的方法
- 微软便携式互动技术OmniTouch让任意表面成为触摸屏
- 点击按钮图片左右滚动