1 Reply Latest reply: Jul 29, 2013 4:49 AM by EJP RSS

    how to set parameter in HttpURLConnection POST method?

    tcbhardwaj

      Hi, I just want to sent parameter in post body. I tried my things but unable to send. I m getting these parameter on web Server as request.getParameter("MODULE"); and request.getParameter("SERVICE");

       

      NOTE : could not change Content-Type

       

      Thanks in advance

       

       

      public class HttpURLConnectionFactory {

       

          private static String V_SESSION_ID = null;

          private static HttpURLConnection httpURLConnection = null;

       

          public static Object getResponseHttp(String module, String service, Object dataObject) throws MalformedURLException, IOException, ClassNotFoundException, VahanException {

              Object result = null;

       

              String urlParameters = "MODULE=" + module + "&SERVICE=" + service; // imroz

       

      //        String urlParameters =

      //        "?MODULE=" + URLEncoder.encode(module, "UTF-8") +

      //        "&SERVICE=" + URLEncoder.encode(service, "UTF-8");

       

      //        String urlParameters = URLEncoder.encode("module", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");

      //        urlParameters += "&" + URLEncoder.encode("service", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

       

              String urlStirng = "http://" + ConfigClient.VAHAN_HTTP_SERVER_HOST + ":" + ConfigClient.VAHAN_HTTP_SERVER_PORT + "/" + ConfigClient.VAHAN_HTTP_SERVER_CONTEXT;

              //urlStirng = urlStirng + "?MODULE=" + module + "&SERVICE=" + service;

              //   urlStirng = urlStirng +urlParameters;

              System.out.println("*  VAHAN HTTP CLIENT : URL STRING :  " + urlStirng);

              URL url = new URL(urlStirng);

              System.out.println("urrrrrrrrrrrrrrrrrrrllllllllllllllllllllllllllllllllllll " + url);

              httpURLConnection = (HttpURLConnection) url.openConnection();

              httpURLConnection.setRequestMethod("POST");

              httpURLConnection.setRequestProperty("Content-Type", "application/x-java-serialized-object");

              //  httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

              httpURLConnection.setRequestProperty("Content-Language", "en-US");

              httpURLConnection.setRequestProperty("Connection", "Keep-Alive");

              httpURLConnection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));// imroz        //   httpURLConnection.setRequestProperty("MODULE", module);

      //       

      //        OutputStream os = httpURLConnection.getOutputStream();

      //        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));

      //        w.write(URLEncoder.encode(urlParameters, "UTF-8"));

      //        w.close();

      //        os.close();

             

              // httpURLConnection.setRequestProperty("SERVICE", service);

       

       

      //         httpURLConnection.addRequestProperty("SERVICE", service);

      //          httpURLConnection.addRequestProperty("MODULE", module);

       

       

       

       

       

              // Manual Setting Cookie attirbute to recognize the request to be from a valid session

              if (V_SESSION_ID != null) {

                  httpURLConnection.setRequestProperty("Cookie", V_SESSION_ID);

              }

              // httpURLConnection.setUseCaches(false);

              httpURLConnection.setUseCaches(true);

              httpURLConnection.setDoInput(true);

              httpURLConnection.setDoOutput(true);

       

       

      //        DataOutputStream wr = new DataOutputStream(

      //                httpURLConnection.getOutputStream());

      //        wr.writeBytes(urlParameters);

      //      

      //

      //        wr.flush();

      //        wr.close();

       

              OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());

       

          //    write parameters

              writer.write(urlParameters);

              writer.flush();

              writer.close();

            

       

      //        String mod = "MODULE=" + URLEncoder.encode(module, "UTF-8");

      //        String serv = "SERVICE=" + URLEncoder.encode(service, "UTF-8");

      //        outputStream.write( (mod +"&" + serv).getBytes()  );

              // reading the response from the Server:  HttpServer : Prashant 13Feb2012

              if (dataObject != null) {

                  OutputStream outputStream = httpURLConnection.getOutputStream();

                  ObjectOutputStream objObjectOutputStream = new ObjectOutputStream(outputStream);

                  objObjectOutputStream.writeObject(dataObject);

                  objObjectOutputStream.flush();

                  objObjectOutputStream.close();

       

              }

       

       

              Map headers = httpURLConnection.getHeaderFields();

              Set headerKeySet = headers.keySet();

              Iterator it = headerKeySet.iterator();

              while (it.hasNext()) {

                  String hKey = (String) it.next();

                  System.out.println(" HeaderName:   " + hKey + "     Header value:  " + headers.get(hKey));

       

              }

       

       

       

       

              String cookieValue = httpURLConnection.getHeaderField("Set-Cookie");

              System.out.println("vvvvvvvvvvvvvvvvvvvvvvvvaaaaaaaaaaaallllllllllllllllll     " + cookieValue);

              if (cookieValue != null) {

                  String jstring = cookieValue.substring(1, cookieValue.indexOf(";"));

                  //   System.out.println(" jstring   :   " + jstring);

                  String remainString = cookieValue.substring(cookieValue.indexOf(";") + 1, cookieValue.length());

                  //  System.out.println(" remainString   :   " + remainString);

                  String sessid = jstring.substring(jstring.indexOf("=") + 1, jstring.length());

                  //  System.out.println(" sessid   :   " + sessid);

                  String finalString = "JSESSIONID=" + sessid + ";" + remainString;

                  V_SESSION_ID = finalString;

       

              }

       

       

              if (httpURLConnection.getResponseCode() == 200) {

                  InputStream inputStream = httpURLConnection.getInputStream();

                  ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);

                  result = objectInputStream.readObject();

                  objectInputStream.close();

              }

              // Every object sent from the server will be of object type

              // checking if it is Exception and thrwing it again in the client's JVM

              //Prashant: 16-JULY2012

              if (result instanceof VahanException) {

                  Exception e = (Exception) result;

                  throw new VahanException(e.getMessage());

              }

       

              // diconnect http

              // httpURLConnection.disconnect();

              return result;

          }