csdn产品小助手 2013-07-09 22:55 采纳率: 0%
浏览 13

来自Servlet的POST请求

I am writing a servlet using eclipse that receives POST request from a client that should do some splitting on the received text, access google geolocation api to get some data and display to the user.

On a localhost, this works perfectly fine. On an actual server (tried with Openshift and CloudBees), this doesn't work. I can see the splitting reply but not the reply from google geolocation service. There is always an error logged into the console from google service. However, the same code works perfectly fine on localhost.

After I receive the POST request in the doPost method of the servlet, I am doing the following to access the Google GeoLocation service:

//Attempting to send data to Google Geolocation Service
            URL url;
            HttpURLConnection connection = null;  
            try {
              //Create connection
              url = new URL("https://www.googleapis.com/geolocation/v1/geolocate?key=MyAPI");
              connection = (HttpURLConnection)url.openConnection();
              connection.setRequestMethod("POST");
              connection.setRequestProperty("Content-Type", "application/json");


              connection.setUseCaches (false);
              connection.setDoInput(true);
              connection.setDoOutput(true);

              //Send request with data (output variable has the JSON data)
              DataOutputStream wr = new DataOutputStream (
                          connection.getOutputStream ());
              wr.writeBytes (output);
              wr.flush ();
              wr.close ();

              //Get Response    
              InputStream is = connection.getInputStream();
              BufferedReader rd = new BufferedReader(new InputStreamReader(is));
              String line;
              StringBuffer response2 = new StringBuffer(); 
              while((line = rd.readLine()) != null) {
                response2.append(line);
                response2.append('');
              }
              rd.close();
//Write to Screen using out=response.getWriter();
              out.println("Access Point's Location = " + response2.toString());

            } catch (Exception e) {

              e.printStackTrace();


            } finally {

              if(connection != null) {
                connection.disconnect(); 
              }

Could you tell me why this is happening and how can I make this work? Should I resort to something like AJAX or is there someother work around? I am relatively new to coding and hence, trying to refrain from learning AJAX at this stage. Please let me if there's any other way of getting this to work

  • 写回答

1条回答 默认 最新

  • 普通网友 2013-07-09 23:26
    关注

    Your localhost has your localhost IP as a sending IP. Openshift et al has the Openshift et al IP as a sending IP. So the Google API says "I have only seen that localhost IP twice before, that's fine!", whereas it says "I have seen this Openshift IP millions of times before! NO REPLY FOR YOU!"

    评论

报告相同问题?

悬赏问题

  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现