http客户端开发get、Post两种方式
来源:互联网 发布:视频同声翻译软件 编辑:程序博客网 时间:2024/06/10 23:14
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");
阅读全文
0 0
- http客户端开发get、Post两种方式
- 两种HTTP连接方式POST&GET的比较
- 两种HTTP连接方式POST&GET的比较
- C# wnform 请求http ( get , post 两种方式 )
- 两种HTTP连接方式POST&GET的比较
- C# winform 请求http ( get , post 两种方式 )
- C# wnform 请求http ( get , post 两种方式 )
- GET与POST比较-使用HTTP协议将客户端的请求传送到服务器端的两种方式
- GET 和 POST两种调用方式
- HTTP的两种提交请求的方式get和post
- http协议的两种主要请求方式post和get
- http消息当中,post和get两种请求方式的区别
- java发送http的两种请求方式——get、post
- 【JavaScript】2.Http中Get与Post两种请求方式的差异
- http post/get 2种使用方式
- 安卓开发-Get和Post两种请求三种方式(模板)
- java http get、post方式
- http请求 get post 方式
- .net 反射访问私有变量和私有方法
- Animator事件
- PAT乙级题1002.写出这个数
- Popupwindow的简单使用
- 用阿里云独享虚拟主机搭建个人网站
- http客户端开发get、Post两种方式
- Android ALSA relevant adb commands
- JVM内存区域
- 常见RGB颜值表和半透明颜色值的写法
- 什么是<!DOCTYPE html>及<!DOCTYPE html>的重要性?
- 主题模型
- sql基础知识总结
- 新装的系统studio识别不到手机
- for(:)循环