Viktor Apoyan
Viktor Apoyan

Reputation: 10755

JAVA Send Post Request which contains XML String

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?

Upvotes: 3

Views: 24210

Answers (2)

Bench Vue
Bench Vue

Reputation: 9310

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;
}

Upvotes: 1

Brett Okken
Brett Okken

Reputation: 6306

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();

Upvotes: 6

Related Questions