java调用(请求)远程服务地址
/**
* 前端Ajax获取管理相对人经纬度信息
* @author jiyanle
* @date 2014-10-31
*/
public String getPosition() {
HttpServletResponse response = ServletActionContext.getResponse();
String jsonStr = "";
PrintWriter out = null;
BufferedReader in = null;
try {
String param = "compNo=341421110525083290®ionCode=340000";
URL url = new URL("http://gis.wsjd.gov.cn:8080/Weisheng/placeinfo/getPlacesAction.action?"+param);
URLConnection conn = url.openConnection();
conn.setUseCaches(true);
//发送POST请求必须设置如下两行;
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-type", "application/octest-stream");
//获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
//发送请求参数
out.print(param);
//flush输出流的缓冲
out.flush();
//定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
jsonStr = in.readLine();
//ajax返回值
out = response.getWriter();
out.print(jsonStr);
}
catch (Exception e) {
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。