Java 跨域请求操作--封装类

风流意气都作罢 提交于 2019-12-08 03:56:43

跨域请求,顾名思义,就是一个站点中的资源去访问另外一个不同域名站点上的资源。 资源可以是一个请求,或一个操作或一个数据流等

 

注:2个封装类都使用到了httpclient.jar包,请到网上搜索下载.

 

(一) 封装类一

 

/**

* 跨域请求操作类
* @author YangZhiFeng
* @version 1.0
* @created 2012-8-10 上午10:56:15
*/
publicclass HttpClientUtil {
/**
* 跨域请求并接收返回的数据
* 参数说明: url:跨域请求的地址 params:需传递的参数,格式为: 参数1=AAA&参数B=BBB&参数C=CCC
* @return String
* @throws IOException
* @throws HttpException
*/
publicstatic String transboundaryRequest(String url, String params)throws HttpException, 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 com.gzedu.util;

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.DefaultHttpMethodRetryHandler;
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;
/**
* 接口抛get/post请求
* @author xiaoming
*
*/
publicclass HttpClientUtils {
privatestatic Integer SC_OK = 200;
/**
* get方式
* @param url
* @param params
* @param timeout
* @return
*/
@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 DefaultHttpMethodRetryHandler());
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;
}
/**
* post方式
* @param url
* @param params
* @param timeout
* @return
*/
@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;
}
/***
* 发送xml文件
* @param url
* @param xml
* @param timeout
* @param encode
* @return
*/
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;
}
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!