Java跨域请求操作---封装类
来源:互联网 发布:海贼王 知乎 编辑:程序博客网 时间:2024/06/06 09:17
跨域请求,顾名思义,就是一个站点中的资源去访问另外一个不同域名站点上的资源。 资源可以是一个请求,或一个操作或一个数据流等
(一) 封装类一
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.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;
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 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;
}
@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;
}
}
0 0
- Java 跨域请求操作--封装类
- Java跨域请求操作---封装类
- Java HTTP请求类封装
- 封装jsonp跨域请求
- HTTP请求封装Java工具类
- Java 操作文本 封装类
- Java文件操作类封装
- Java文件操作类封装
- Java文件操作类封装
- xUtils封装http请求操作
- Okhttp请求封装类
- 自己封装了一个Okhttp网络请求,操作简单,可区分一个类中的不同请求
- 封装Java数据库操作的类
- Java操作Word文档封装类
- 转载【封装Java数据库操作的类】
- Java操作Word文档封装类
- JAVA文件操作封装工具类
- JAVA时间操作工具类封装
- Textview设置不同样式
- Win32字符串编码格式转化
- Zendstuido汉化方法
- 如何安装和配置Mantis
- 杭电5620
- Java跨域请求操作---封装类
- 【SQL】 DROP DELATE区别
- 线段树专题#1_蒟蒻训练历程记录_HDU1166
- java中图片简单操作
- JAVA API 自带图片压缩
- Java Notes: Java的this和static
- OpenCV各个模块/各个文件夹的含义
- 02-线性结构3 Pop Sequence
- 利用Mockito进行mock方法时有无返回值的处理