所需jar包:
commons-httpclient-3.0.jar
commons-codec-1.3.jar
s
/**
* get 方式
* @param url 路径
* @param nameValuePairs[] 参数
* @return String
* @throws IOException
*/
public static String callGet(String url, NameValuePair[] nameValuePairs) throws IOException {
HttpClient httpClient = null;
GetMethod getMethod = null;
try {
httpClient = new HttpClient();
getMethod = new GetMethod(url);
if (null != nameValuePairs && nameValuePairs.length > 0)
getMethod.setQueryString(nameValuePairs);
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode == HttpStatus.SC_OK) {
// 读取内容
return IOUtil.getStrByInputStrean(getMethod.getResponseBodyAsStream());
}
} catch (HttpException e) {
throw new HttpException(e.getMessage());
} catch (IOException e) {
throw new IOException(e.getMessage());
} finally {
getMethod.releaseConnection();// 释放
}
return null;
}
/**
* post 方式
* @param url 路径
* @param nameValuePairs[] 参数
* @return String
* @throws IOException
*/
public static String callPost(String url, NameValuePair[] nameValuePairs) throws IOException {
HttpClient httpClient = null;
PostMethod postMethod = null;
try {
httpClient = new HttpClient();
postMethod = new PostMethod(url);
if (null != nameValuePairs && nameValuePairs.length > 0)
postMethod.addParameters(nameValuePairs);
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_OK) {
// 读取内容
// byte[] responseBody = postMethod.getResponseBody();
return IOUtil.getStrByInputStrean(postMethod.getResponseBodyAsStream());
// return new String(responseBody);
}
} catch (HttpException e) {
throw new HttpException(e.getMessage());
} catch (IOException e) {
throw new IOException(e.getMessage());
} finally {
postMethod.releaseConnection();// 释放
}
return null;
}
设置get方法请求超时为 5 秒
- GetMethod getMethod=new GetMethod(url);
- getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);
设置 Http 连接超时为5秒
- HttpClient httpClient=new HttpClient();
- httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
设置连接超时和请求超时,这两个超时的意义不同,需要分别设置。
by : http://www.blogjava.net/guangnian0412/archive/2006/04/11/40529.htmls
问题:
你需要使用HTTP POST 方法来向一个servlet传递参数。
讨论:
创建一个 PostMethod 对象,然后调用 setParameter() 或 addParameter() 方法设置参数。 PostMethod 对象将会传送一个 Content-Type 头为 application/x-www-form-urlencoded 的请求,并且参数将在请求body中被传送。在下列的例子中演示了用 PostMethod 对象传递参数的用法:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
HttpClient client = new HttpClient( );
// Create POST method
String url = " http://www.discursive.com/cgi-bin/jccook/param_list.cgi " ;
PostMethod method = new PostMethod( url );
// Set parameters on POST
method.setParameter( " test1 " , " Hello World " );
method.addParameter( " test2 " , " This is a Form Submission " );
method.addParameter( " Blah " , " Whoop " );
method.addParameter( new NameValuePair( " Blah " , " Whoop2 " ) );
// Execute and print response
client.executeMethod( method );
String response = method.getResponseBodyAsString( );
System.out.println( response );
method.releaseConnection( );param_list.cgi CGI脚本会对所以接收到的参数进行回显,从下面的输出中,你可以看到传递给CGI脚本的三个参数: