HTTPS POST

Discussions

General J2EE: HTTPS POST

  1. HTTPS POST (1 messages)

    Hi there!
    I need to post a string (A xml file) to a https server and get the servers response.
    I can connect to server, but it seems I can´t post, the string (file) I want to, ´cause when I get the response, it comes as if I didn´s post anything.
    I don´t need to use a OutputStreamWriter, ´cause I already have the object (string) I want to post, but here I´m using it. But how does the remote server knows the name of the request Attribute? Should I do that inside a servlet?
    Can someone help me out?
    Thnk a lot!
    Here´s the class:

    import java.io.*;
    import java.net.*;
    import java.security.cert.X509Certificate;
    import javax.net.ssl.*;
    import java.security.*;

    public class Test
        implements javax.net.ssl.X509TrustManager {
      public static void main(String[] args) {
        URL url;
        StringBuffer buffer;
        String line;
        int responseCode = 0;
        SSLContext sc = null;
        try {
          sc = SSLContext.getInstance("SSLv3");
        }
        catch (NoSuchAlgorithmException ex) {
       ex.printStackTrace();
        }
        TrustManager[] tma = {
            new Test()};
        try {
          sc.init(null, tma, null);
        }
        catch (KeyManagementException ex1) {
          ex1.printStackTrace();
        }
        SSLSocketFactory ssf = sc.getSocketFactory();
        HttpsURLConnection.setDefaultSSLSocketFactory(ssf);
        HttpsURLConnection connection = null;
        InputStream input;
        BufferedReader dataInput;
        try {
          url = new URL(
              "https://www.myserver.com/index.jsp");

          connection = (HttpsURLConnection) url.openConnection();
          connection.setDoInput(true);
          //connection.addRequestProperty("XML", "test");
          //connection.setRequestProperty("XML", "test");
          connection.setRequestMethod("POST");
          connection.setDoOutput(true);
          connection.setRequestProperty("Content-type", "text/xml");
          
          int c = 0;
          OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
          FileInputStream fis = new FileInputStream("test.xml");
          while ( (c = fis.read()) != -1) {
            osw.write(c);
          }
          osw.close();
          System.out.println(connection.getResponseMessage());

          responseCode = connection.getResponseCode();
        }
        catch (Exception e) {
          System.err.println(e);
        }
        String response = "";
        try {
          buffer = new StringBuffer();
          input = connection.getInputStream();
          dataInput = new BufferedReader(new InputStreamReader(input));
          while ( (line = dataInput.readLine()) != null) {
            buffer.append(line);
            buffer.append('\n');
          }
          response = (String) buffer.toString().trim();
        }
        catch (Exception e) {
          System.err.println(e);
        }
        System.out.println(response);
      }

      public void checkClientTrusted(X509Certificate[] chain, String authType) {
      }

      public void checkServerTrusted(X509Certificate[] chain, String authType) {
      }

      public X509Certificate[] getAcceptedIssuers() {
        return null;
      }
    }

    Threaded Messages (1)

  2. HTTPS POST[ Go to top ]

    Hi there!I need to post a string (A xml file) to a https server and get the servers response.
    Use the Jakarta Commons HttpClient

    http://cvs.apache.org/viewcvs.cgi/jakarta-commons/httpclient/src/examples/PostXML.java?rev=1.11