通过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("<", "<").replaceAll(">", ">");
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
- 通过url读取XML
- 通过url读取文件
- 通过URL读取文件
- 通过URL读取图片
- 通过URL读取文件内容
- 通过JDom读取XML文件
- kettle通过xml读取数据库连接
- c#---通过xml读取数据库
- 通过xpath读取xml节点
- java通过url读取网络图片
- android通过url读取网络图片
- Android通过URL读取网络图片
- java通过URL读取json数据
- android通过url读取网络图片
- java通过URL读取json数据
- java通过URL读取json数据
- 直接读取xml文件,站点伪Url
- java 读取url获取的xml
- How to Change Login User in SharePoint2013?
- hdu 1548 A strange lift(Dijkstra+合理转换)
- 程序员需要知道的各类官方文档地址
- UIWebView无法用loadRequest加载页面 错误提示:App Transport Security has blocked a cleartext HTTP ...
- iOS学习笔记——文件操作(NSFileManager)
- 通过url读取XML
- Oracle12c中性能优化&功能增强新特性之临时undo
- css笔记重点
- Java的版本、特性和历史
- PackageManagerService(Android5.1)深入分析(一)构造函数
- Spring学习笔记---2-IOC详解
- 关于N*N方格从(1,1)到(N,N)的最短距离
- printf、sscanf进行16进制数据转换问题
- Hadoop与Spark常用配置参数总结