JAVA Send Post Request which contains XML String
18,885
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();
Author by
Viktor Apoyan
Updated on June 05, 2022Comments
-
Viktor Apoyan almost 2 years
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?
-
Roman C over 9 yearsFirst you need to serialize to JSON then send over HTTP.
-
Khary Mendez over 9 yearsNot sure why you are appending "request=" to the XML, other than that it looks ok.
-
Viktor Apoyan over 9 years@RomanC can you bring some example as a answer of how to send XML string via http connection in correct way?
-
Viktor Apoyan over 9 years@kharyam you mean
String urlParameters = XMLSRequest;
this is Okay ? -
Roman C over 9 yearsYou should not use http connection.
-
Viktor Apoyan over 9 years@RomanC why not to use http?
-
Viktor Apoyan over 9 yearsI just need to send the request to server which contains XML as a string. This what I need to to Send as string
-
Khary Mendez over 9 years@ViToBrothers yes. Or you can just delete that line and do wr.writeBytes(XMLRequest)
-
Roman C over 9 years@ViToBrothers Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.
-
Viktor Apoyan over 9 years@RomanC I need to send POST request which contains XML as a string.
-
Viktor Apoyan over 9 years@RomanC I edit question you can check
-
Brett Okken over 9 yearsAre you wanting to send XML in the body of the request (with Content-Type of text/xml)? Or are you wanting to send a parameter (Content-Type application/x-www-form-urlencoded) whose value is xml and send it in the body of the request to avoid url length issues?
-
Viktor Apoyan over 9 years@BrettOkken I think for me better way is to send it in the body, as my string is very long (
XMLString
),so I can send it in a body of request also. -
Brett Okken over 9 years@ViToBrothers My question was what Content-Type are you wanting to use? What does the service you are calling expect?
-
Viktor Apoyan over 9 years@BrettOkken actually server does not exists, so I just need to simulate ending of XML String
-
Viktor Apoyan over 9 years@BrettOkken, so I need just to send XML String to not existing server.
-
-
Thilina Sampath over 8 yearsurlConnection not working my jax-rs web service which lib add for this work