3

Problem Description

I'm trying to write a code which is sending POST request to the server. As server yet doesn't exist I can't test this part of code. With the request I must send XML as a String which look likes the string below:

String XMLSRequest = "<?xml version="1.0" encoding="UTF-8" standalone="no"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><AuthenticationHeader><Username>Victor</Username><Password>Apoyan</Password></AuthenticationHeader></soapenv:Body></soapenv:Envelope>"

Solution

String url = "https://testurl.com/somerequest";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

String urlParameters = String.format("request=%s", XMLSRequest);

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

Question

Is this right way to send String (XML like string) as POST request to the server?

16
  • First you need to serialize to JSON then send over HTTP. Commented Aug 3, 2014 at 12:24
  • Not sure why you are appending "request=" to the XML, other than that it looks ok. Commented Aug 3, 2014 at 12:27
  • @RomanC can you bring some example as a answer of how to send XML string via http connection in correct way? Commented Aug 3, 2014 at 12:27
  • @kharyam you mean String urlParameters = XMLSRequest; this is Okay ? Commented Aug 3, 2014 at 12:28
  • You should not use http connection. Commented Aug 3, 2014 at 12:29

2 Answers 2

6

To POST a SOAP request, you would want to write the xml to the body of the request. You do not want to write it as a parameter of the request.

String url = "https://testurl.com/somerequest";
URL obj = new URL(url);
urlConnection con = (HttpsURLConnection) obj.openConnection();

// add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");

// Send post request
con.setDoOutput(true);
OutputStream os = con.getOutputStream(); //get output Stream from con
os.write( XMLSRequest.getBytes("utf-8") );
os.close();
Sign up to request clarification or add additional context in comments.

1 Comment

urlConnection not working my jax-rs web service which lib add for this work
1

Specially this code works well for sending XML string in SOAP calling

import java.net.HttpURLConnection;
import java.net.URL;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public static String sendXmlString(String xmlString){
    String xmlResponceString = "";
    try {
        // Replace here with your target URL
        URL url = new URL("http://www.dneonline.com/calculator.asmx");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // Set timeout as per needs
        connection.setConnectTimeout(20000);
        connection.setReadTimeout(20000);

        // Set DoOutput to true if you want to use URLConnection for output.
        // Default is false
        connection.setDoOutput(true);

        connection.setUseCaches(true);
        connection.setRequestMethod("POST");

        // Set Headers
        connection.setRequestProperty("Accept", "*/xml");
        connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        connection.setRequestProperty("User-Agent", "");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 
        // Write XML
        OutputStream outputStream = connection.getOutputStream();
        byte[] b = xmlString.getBytes("UTF-8");
        outputStream.write(b);
        outputStream.flush();
        outputStream.close();

        // Read XML
        InputStream inputStream = connection.getInputStream();
        byte[] res = new byte[2048];
        int i = 0;
        StringBuilder response = new StringBuilder();
        while ((i = inputStream.read(res)) != -1) {
            response.append(new String(res, 0, i));
        }
        inputStream.close();
        xmlResponceString = new String(response.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return xmlResponceString;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.