通过url读取XML

来源:互联网 发布:光明骑士和游侠数据 编辑:程序博客网 时间:2024/05/29 18:15


import java.io.IOException;
import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;


public class HttpClientUtil {
/***
* 发送http POST请求
* @param url 发送url,为服务商平台的链接
* @throws IOException
*/
public static String sendHttp(String url, String params) {
/** 配置http请求的配置 */
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)/** 设置超时时间 */
.setConnectionRequestTimeout(12000)/** 设置链接时间 */
.build();
/** 配置http请求客户端 */
CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
/** HttpRequest对象 */
HttpPost post = null;
/** HttpResponse对象 */
CloseableHttpResponse response = null;
/** response返回的结果 */
String responseString = null;
/** 返回的状态吗 */
int responseStatus = 0;
try {
post = new HttpPost(url);
post.setHeader(HttpHeaders.CONTENT_ENCODING, "UTF-8");
StringEntity entity = new StringEntity(params,"UTF-8");
post.setEntity(entity);

/** 接收返回对象 */
response = client.execute(post);
responseStatus = response.getStatusLine().getStatusCode();
/** 返回正确的状态吗 */
if (responseStatus == HttpStatus.SC_OK) {
HttpEntity httpEntity = response.getEntity();
if (null != httpEntity) {
responseString = EntityUtils.toString(httpEntity);
httpEntity.getContent().close();
}
} else {

}


} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
try {
if(null!=response){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
requestConfig = null;
client = null;
}
return responseString;
}



public static String sendHttp(String url, List<NameValuePair> params){
/** 配置http请求的配置 */
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)/** 设置超时时间 */
.setConnectionRequestTimeout(12000)/** 设置链接时间 */
.build();
/** 配置http请求客户端 */
CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
/** HttpRequest对象 */
HttpPost post = null;
/** HttpResponse对象 */
CloseableHttpResponse response = null;
/** response返回的结果 */
String responseString = null;
/** 返回的状态吗 */
int responseStatus = 0;
try {
post = new HttpPost(url);
post.setHeader(HttpHeaders.CONTENT_ENCODING, "UTF-8");
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
/** 接收返回对象 */
response = client.execute(post);
responseStatus = response.getStatusLine().getStatusCode();
/** 返回正确的状态吗 */
if (responseStatus == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (null != entity) {
responseString = EntityUtils.toString(entity);
entity.getContent().close();
}
} else {
/** do nothing */
}


} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
try {
if(null!=response){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
requestConfig = null;
client = null;
}
return responseString;
}





}



import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;


import org.apache.log4j.Logger;


import sun.rmi.runtime.Log;






public class Test1{

private static Logger logger = Logger.getLogger(Log.class);
public static void main(String[] args){
String url="";
String result = HttpClientUtil.sendHttp(url, "").replaceAll("&lt;", "<").replaceAll("&gt;", ">");
System.out.println(result);
try {
result= URLEncoder.encode(result, "ISO8859-1");
result = URLDecoder.decode(result,"UTF-8");
System.out.println(result);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
















0 0
原创粉丝点击