跨域请求
,顾名思义,就是一个站点中的资源去访问另外一个不同域名站点上的资源。 资源可以是一个请求,或一个操作或一个数据流等
(一) 封装类一
publicclass HttpClientUtil {
publicstatic String transboundaryRequest(String url, String params)throwsHttpException, IOException {
//请求方法
String response=new String();
HttpClient client =new HttpClient();
//解决乱码问题
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
PostMethod method =new PostMethod(ObjectUtils.toString(url));
String[] paramList = params.split("&");
for (int i = 0; i < paramList.length; i++) {
String[] param = paramList[i].split("=");
if(param[0]!=""){
if(param.length>1){
method.addParameter(param[0],ObjectUtils.toString(param[1]));
}else{
method.addParameter(param[0],"");
}
}
}
client.executeMethod(method);
response = method.getResponseBodyAsString();
return response;
}
}
(二)封装类二
package
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHa ndler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
publicclass HttpClientUtils {
privatestatic Integer SC_OK = 200;
@SuppressWarnings("unchecked")
publicstatic String doHttpGet(String url, Map params,int timeout, String encode) {
String responseMsg ="";
HttpClient httpClient =new HttpClient();
httpClient.getParams().setContentCharset(encode);
StringBuffer sb =new StringBuffer();
if (params !=null && !params.isEmpty()) {
Iterator iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry entry = (Entry) iterator.next();
sb.append(entry.getKey().toString() +"="
+ entry.getValue().toString() +"&");
}
url = url +"?" + sb.substring(0, sb.length()-1);
}
GetMethod getMethod =new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHa ndler());
try {
httpClient.getHttpConnectionManager ().getParams()
.setConnectionTimeout(timeout);
int num = httpClient.executeMethod(getMethod);
if (num ==SC_OK) {
InputStream is = getMethod.getResponseBodyAsStream();
BufferedReader br =new BufferedReader(new InputStreamReader(is));
String tempbf;
StringBuffer html=new StringBuffer(1000);
while((tempbf=br.readLine())!=null){
html.append(tempbf);
}
responseMsg = html.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
return responseMsg;
}
@SuppressWarnings("unchecked")
publicstatic String doHttpPost(String url, Map params,int timeout, String encode) {
String responseMsg ="";
HttpClient httpClient =new HttpClient();
httpClient.getParams().setContentCharset(encode);
PostMethod postMethod =new PostMethod(url);
if (params !=null && !params.isEmpty()) {
Iterator iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry entry = (Entry) iterator.next();
postMethod.addParameter(entry.getKey().toString(), entry
.getValue().toString());
}
}
try {
httpClient.getHttpConnectionManager ().getParams()
.setConnectionTimeout(timeout);
int num = httpClient.executeMethod(postMethod);
if (num ==SC_OK) {
InputStream is = postMethod.getResponseBodyAsStream();
BufferedReader br =new BufferedReader(new InputStreamReader(is));
String tempbf;
StringBuffer html=new StringBuffer(1000);
while((tempbf=br.readLine())!=null){
html.append(tempbf);
}
responseMsg = html.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
return responseMsg;
}
publicstatic String doHttpPostXml(String url, String xmlString,int timeout, String encode){
String responseString ="";
HttpClient httpClient =new HttpClient();
httpClient.getHttpConnectionManager ().getParams().setConnectionTimeout(timeout);
PostMethod postMethod =new PostMethod(url);
try {
postMethod.setRequestHeader("Content-Type","text/xml,charset="+encode);
postMethod.setRequestEntity(new StringRequestEntity(xmlString,"text/xml",encode));
int statusCode = httpClient.executeMethod(postMethod);
if(statusCode ==SC_OK){
InputStream is = postMethod.getResponseBodyAsStream();
BufferedReader br =new BufferedReader(new InputStreamReader(is));
String tempbf;
StringBuffer html=new StringBuffer(1000);
while((tempbf=br.readLine())!=null){
html.append(tempbf);
}
responseString = html.toString();
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseString;
}
}