比username=ss,在我的程序中可以接收这个参数是没有问题的,怎么把这个参数发送给另一个web程序?只能通过httpclient重新拼接参数吗?
这种情况下 有两种方案,
1 一个是在客户端用ajax去做。可以发送两个ajax请求,参数组装成json,参数的组装不是很麻烦。
2 在服务器端用httpClient或类似的功能,httpClient,也不用拼接字符串,可以使用NameValuePair类,进行参数的封装。
[code="java"]
/**
import java.io.IOException;
import java.util.List;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*Class HttpClientUtil
*Descripton goes here
*@author:zhjt
*@version: Revision:2.0.0, 2012-4-5 下午04:49:55
*/
public final class HttpClientUtil
{
static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
static int CONNECTIONTIMEOUTMILLIS = 10000;
static int SOCKETTIMEOUTMILLIS = 10000;
/**
* 执行Http的返回结果<Get>
*/
public static String executeService(String url)throws Exception {
logger.debug("SSOService executeService start!");
String result = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
//设置超时事件
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),CONNECTIONTIMEOUTMILLIS);
HttpConnectionParams.setSoTimeout(httpclient.getParams(), SOCKETTIMEOUTMILLIS);
try {
HttpGet httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
result = responseBody;
logger.debug("SSOService executeService end!");
}catch(Exception e){
logger.debug("SSOService executeService error:"+e);
} finally {
httpclient.getConnectionManager().shutdown();
}
return result;
}
/**
* 执行Http的返回结果<POST>
*/
public static String executeServicePOST(String url, List nvps)
throws IOException {
logger.debug("SSOService executeServicePOST start!");
String result = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
//设置超时事件
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), CONNECTIONTIMEOUTMILLIS);
HttpConnectionParams.setSoTimeout(httpclient.getParams(), SOCKETTIMEOUTMILLIS);
try {
HttpPost httpost = new HttpPost( url + "?");
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpost, responseHandler);
result=responseBody;
logger.debug("SSOService executeServicePOST end!");
}catch(Exception e){
logger.debug("SSOService executeServicePOST error:"+e);
} finally {
httpclient.getConnectionManager().shutdown();
}
return result;
}
}
[/code]
这是个发http请求的例子 你也可以网上搜搜更好的例子