当前位置: 代码迷 >> J2SE >> HttpURLConnection webservice.webxml 请求数据解决方案
  详细解决方案

HttpURLConnection webservice.webxml 请求数据解决方案

热度:469   发布时间:2016-04-23 20:07:44.0
HttpURLConnection webservice.webxml 请求数据
各位大牛,我今天在做一个使用HttpURLConnection从http://www.webxml.com.cn/请求关于火车时刻表的信息,使用的是get请求,在网站打开的数据查询时正常的,如图,但是我将请求的连接使用到我写的程序当中去的时候就会出现没有找到数据的问题,但是内容的xml头部确实返回回来了,如图,不知道问题出在哪里,找了很多资料也没用解决,求助,代码如下

public class GetTrainTime {
private final static String BaseURL = "http://webservice.webxml.com.cn//WebServices/TrainTimeWebService.asmx/getStationAndTimeByStationName?";

public static void getTrainTime(String startStation, String arriveStation){
String url = BaseURL + "StartStation=" + startStation + "&ArriveStation=" + arriveStation + "&UserID=";
try {
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(50000);
conn.connect();
System.out.println(conn.getURL().toString());
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStream in = conn.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];//设置缓冲区
int len = 0;
try {
while((len = (in.read(buffer))) != -1){
outputStream.write(buffer, 0, len);
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}

try {
outputStream.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}

String times = new String(outputStream.toByteArray(), "UTF-8");
System.out.println(times);
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
getTrainTime("长沙", "北京");
}
}


,求助,求助
------解决思路----------------------
String url = BaseURL + "StartStation=" + URLEncoder.encode(startStation,"UTF-8") + "&ArriveStation=" +URLEncoder.encode(arriveStation,"UTF-8")  + "&UserID=";

发送的时候要编码
  相关解决方案