404TecZ
404TecZ

Reputation: 11

Configuration of Java HttpsURLConnection

currently im working on a system to send credentials and a XML selected from a TextPane or from a file, i would get the path to it with a FileChooser. my question is if someone knows an explanation how to configure the HttpsUrlConnection. I understand that its done by .setRequestProperty or .addRequestProperty but the server is throwing this error

ActDelivery_HTTP.Utils:getEncodingFromPartner(/0/1) ActDelivery_HTTP.Inbound:receiveXML(/0/0/0) UTF-8

I have researched a lot but I'm not that good in java. That's why I'm asking if someone could explain the configuration of a HttpsUrlConnection.

                    try {

                        Authenticator.setDefault (new Authenticator() {
                            protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication (textUser.getText(), textPass.getText().toCharArray());
                            }
                        });

                        URL myurl = new URL(httpsURL);
                        HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
                        con.setRequestMethod("POST");

                        con.setRequestProperty("Content-length", URLEncoder.encode(textXML.getText(), "UTF-8")); 
                        con.setRequestProperty("Content-Type","text/xml; charset=UTF-8");
                        con.setRequestProperty("Https-Agent", ""); 
                        con.setRequestProperty("Content", URLEncoder.encode(textXML.getText(), "UTF-8"));   
                        con.setRequestProperty("Dest-Port", "443");                                                                                 // I'm not sure how to add the xml as request 
                        con.setDoOutput(true);                                                              // out of a text box or just the file at all
                        con.setDoInput(true); 

                        DataOutputStream output = new DataOutputStream(con.getOutputStream());  


                        output.writeBytes(textXML.getText());

                        output.close();

                        DataInputStream input = new DataInputStream( con.getInputStream() ); 



                        for( int c = input.read(); c != -1; c = input.read() ) 
                        System.out.print( (char)c ); 
                        input.close();

                        System.out.println("Resp Code:"+con .getResponseCode()); 
                        System.out.println("Resp Message:"+ con .getResponseMessage()); 

Upvotes: 1

Views: 231

Answers (1)

Rajesh Jose
Rajesh Jose

Reputation: 342

What configuration you are referring to? And what issue you are facing? And what error you are getting?

If you want to sent some content in the HTTP POST body, you should write that to the output stream of the URL connection.

output.writeBytes("mytextcontent");

Depending what the content type, u will have to use appropriate api or output stream.

Upvotes: 1

Related Questions