0

I am making a post request using HttpUrlConnection in java, I am getting response on postman but not when I call it from java code.

Below is my code:

public static JSONObject sendPostRequest(String requestURL,
            Map<String, String> params,Map<String,String>headers) throws IOException {
            StringBuffer response = new StringBuffer();

            URL url = new URL(requestURL);
            HttpURLConnection   httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestMethod("POST");
            httpConn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded");


            StringBuffer requestParams = new StringBuffer();

            if (params != null && params.size() > 0) {



                for (Map.Entry<String,String> entry : headers.entrySet()) 
                    httpConn.setRequestProperty(entry.getKey(), entry.getValue());

                httpConn.setDoOutput(true);
                // creates the params string, encode them using URLEncoder
                Iterator<String> paramIterator = params.keySet().iterator();
                while (paramIterator.hasNext()) {
                    String key = paramIterator.next();
                    String value = params.get(key);
                    requestParams.append(URLEncoder.encode(key, "UTF-8"));
                    requestParams.append("=").append(
                            URLEncoder.encode(value, "UTF-8"));
                    requestParams.append("&");
                }

                // sends POST data
                OutputStreamWriter writer = new OutputStreamWriter(
                        httpConn.getOutputStream());
                writer.write(requestParams.toString());
                writer.flush();

                int responseCode = httpConn.getResponseCode();

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(httpConn.getInputStream()));
                String inputLine;


                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                //print result
                System.out.println(response.toString());

            }

return new JSONObject().parse(response.toString()); }

The response code : 200 Response string :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Error</title></head><body>     <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%" >        <!-- <tr> -->         <!-- <td class="dialog_header" colspan="3"> -->             <!-- <table cellpadding="0" cellspacing="0" border="0" width="100%"> -->                  <!-- <tr> -->                 <!-- <td /></td> -->                    <!-- <td  style="font-size:15px;"><span >Internal Server Error</span></td> -->                  <!-- <td ></td> -->               <!-- </tr> -->                <!-- </table> -->           <!-- </td> -->        <!-- </tr> -->          <tr>          <td style="width: 20px;"></td>          <td ><br />             The requested web page either has an error or does not exist.<br /><br />               Please contact your system administrator or customer support.<br /><br />           </td>           <td style="width: 20px;"></td>        </tr>     </table>     </body></html>

0 Answers0