简体   繁体   中英

POST Requests from a Servlet

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('\r');
              }
              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

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!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM