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 yalmip kkt 双层优化问题
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题