I need to send a raw SOAP request to server. My request looks like
POST http://10.76.243.43:8080/registry/services/xds-iti18 HTTP/1.1
Accept-Encoding: gzip,
Can you try like this: you need to replace the xmldata with yours:
try {
String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelopey> your reuest</soapenv:Envelope>";
String hostname = "10.76.243.43";
int port = 8080;
InetAddress addr = InetAddress.getByName(hostname);
Socket sock = new Socket(addr, port);
//Send header
String path = "registry/services/xds-iti18";
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF-8"));
wr.write("POST "+path + " HTTP/1.1\r\n");
wr.write("Host: "+hostname+":"+port+"\r\n");
wr.write("Content-Length: "+ xmldata.length() + "\r\n");
wr.write("Content-Type: application/soap+xml; charset=\"utf-8\"\r\n");
wr.write("\r\n"); //Send data
wr.write(xmldata);
wr.flush();
// Response
BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println("Response:"+line);
}
} catch (Exception e) {
e.printStackTrace();
}
Instead of socket you can use java HttpURLConnection, Here you must have set SOAPAction header attribute properly in you case it is "AdhocQueryRequest" or you can cross check from your wsdl, below is tentative untested code.
URL url = new URL(SOAPUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
FileInputStream fin = new FileInputStream(xmlFile2Send);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
// Copy the SOAP file to the open connection.
copy(fin,bout);
fin.close();
byte[] b = bout.toByteArray();
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty( "Content-Length",
String.valueOf( b.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction",SOAPAction);
httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
// Everything's set up; send the XML that was read in to b.
OutputStream out = httpConn.getOutputStream();
out.write( b );
out.close();
// Read the response and write it to standard out.
InputStreamReader isr =
new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
I've found an answer. This code worked for me
byte[] encoded = Files.readAllBytes(Paths.get("C:\\Users\\timofb\\Documents\\test.txt"));
String soapXml = new String(encoded, StandardCharsets.UTF_8);
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
java.net.URL endpoint = new URL("http://" + ip + "/cs-repository/services/xds-iti41");
SOAPConnection connection = soapConnectionFactory.createConnection();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(encoded));
AttachmentPart attachment = message.createAttachmentPart();
attachment.setContent("sm_content", "text/plain");
attachment.setContentId("1.9f910338bf0cac0e783bfdec7e53be9237684caa8f8f4e6d@apache.org");
message.addAttachmentPart(attachment);
SOAPMessage response = connection.call(message, endpoint);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.writeTo(out);
String strMsg = new String(out.toByteArray());
return strMsg;