java中用什么方法调用远程url并接收其返回的xml或json

比如我有一个火车票查询的api,它的返回值是xml,现在不用ajax。用java代码在后台怎么实现?

可以直接通过get方式或者post方式工具类来实现:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
* Http 工具类 ,依赖org.apache.http.* 和 org.apache.commons.httpclient.*
*
*
* @date 2015 -09-14 上午9:56:10
*
* @author gaodebao
*
*/
public class CMBCHttpUtil {

private static final String DEFAULT_ENCODING = "GBK";

/**
* GET方式请求
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String get(String uri) throws ClientProtocolException,
IOException {
HttpGet httpGet = new HttpGet(uri);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
}
throw new IOException("status is " + statusCode);
}

public static String get(String uri, Map<String, String> paramMap)
throws ClientProtocolException, IOException {

StringBuilder sb = new StringBuilder(uri);
if (paramMap != null) {
boolean isBegin = true;
for (String key : paramMap.keySet()) {
if (isBegin) {
sb.append("?").append(key).append("=")
.append(paramMap.get(key));
isBegin = false;
} else {
sb.append("&").append(key).append("=")
.append(paramMap.get(key));
}

}
}
HttpGet httpGet = new HttpGet(sb.toString());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
}
throw new IOException("status is " + statusCode);
}

/**
* GET方式请求https
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String httpsGet(String uri, String keyFile, String keyPwd)
throws Exception {
HttpGet httpGet = new HttpGet(uri);
HttpClient httpClient = newHttpsClient(keyFile, keyPwd);
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
}
throw new IOException("status is " + statusCode);
}

/**
* POST方式请求
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @param paramMap
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String post(String uri, Map<String, String> paramMap)
throws ClientProtocolException, IOException {

System.out.print(uri);

HttpPost httpPost = new HttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (paramMap != null) {
for (String key : paramMap.keySet()) {
params.add(new BasicNameValuePair(key, paramMap.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,
DEFAULT_ENCODING));
}
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity());
}
throw new IOException("status is " + statusCode);
}

/**
* POST方式请求
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @param paramMap
* @param headers
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String post(String uri, Map<String, String> paramMap,
Map<String, String> headers) throws ClientProtocolException,
IOException {
HttpPost httpPost = new HttpPost(uri);
if (headers != null) {
for (String key : headers.keySet()) {
httpPost.setHeader(key, headers.get(key));
}
}
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (paramMap != null) {
for (String key : paramMap.keySet()) {
params.add(new BasicNameValuePair(key, paramMap.get(key)));
}
httpPost.setEntity(new ByteArrayEntity(paramMap.get("reqData").getBytes("UTF-8")));
// httpPost.setEntity(new UrlEncodedFormEntity(params,
// DEFAULT_ENCODING));
}
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity());
}
throw new IOException("status is " + statusCode);
}

public static String post(String uri, String contentType, String content)
throws Exception {
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();

PostMethod post = new PostMethod(uri);

RequestEntity entity = new StringRequestEntity(content, contentType,DEFAULT_ENCODING);
post.setRequestEntity(entity);

int statusCode = client.executeMethod(post);
if (statusCode == 200) {
return post.getResponseBodyAsString();
}
throw new IOException("status is " + statusCode);
}

// public static String post(String uri, String contentType, String content)
// throws Exception {
// org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
//
// PostMethod post = new PostMethod(uri);
//
// RequestEntity entity = new StringRequestEntity(content, contentType,DEFAULT_ENCODING);
// post.setRequestEntity(entity);
//
// int statusCode = client.executeMethod(post);
// if (statusCode == 200) {
// return post.getResponseBodyAsString();
// }
// throw new IOException("status is " + statusCode);
// }

/**
* POST方式请求https
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @param paramMap
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String httpsPost(String uri, Map<String, String> paramMap,
String keyFile, String keyPwd) throws ClientProtocolException,
IOException, Exception {
HttpPost httpPost = new HttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (paramMap != null) {
for (String key : paramMap.keySet()) {
params.add(new BasicNameValuePair(key, paramMap.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,
DEFAULT_ENCODING));
}
HttpResponse httpResponse = newHttpsClient(keyFile, keyPwd).execute(
httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity());
}
throw new IOException("status is " + statusCode);
}

/*
* 新建httpsClient
*/
private static HttpClient newHttpsClient(String keyFile, String keyPwd)
throws Exception {
KeyStore trustStore = KeyStore.getInstance("BKS");
trustStore.load(new FileInputStream(new File(keyFile)),
keyPwd.toCharArray());
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 8443);
HttpClient client = new DefaultHttpClient();
client.getConnectionManager().getSchemeRegistry().register(sch);
return client;
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2018-02-28
StringBuffer document = new StringBuffer();
try{
URL url = new URL("http://www.baidu.com");//远程url
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
document.append(line + " ");
reader.close();
}catch(MalformedURLException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
String xml = document.toString();//返回值本回答被提问者和网友采纳
第2个回答  2012-04-11
可以用Apache的http client来做,挺好用的,可以自己搜一下,
或者参考一下http://blog.csdn.net/javavenus/article/details/6560997
第3个回答  2012-04-11
没太明白 是怎么返回接受在 页面么? 比如json?你调用了API以后 直接使用struts返回页面一个result不就可以了么
第4个回答  2012-04-11
用java解析xml的api啊追问

是一个url形式的api,我的意思是用哪个方法把可以这个url发送出去。。

追答

用URL或者String封装好这个url的内容,用流的形式发过去

相似回答