首页 > 代码库 > java调用(请求)远程服务地址

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&regionCode=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;
 }

java调用(请求)远程服务地址