0

Hello I'm a bit new at java and I need help.

Could someone explain to me how to make a request like this:

curl --data @FILE.xml -H 'content-type: application/xml' 'Host: URL' --cert ./FILE.crt:FILEPASS --key ./FILE.pem URL-REQUEST

(this already working at linux terminal) using java code. This sentence sends a XML file.

Thank you and have a nice day.

Martín Zaragoza
  • 1,523
  • 6
  • 17
  • Hello, yes this has a similar part but in that post he don't have the SSL part that is making me same problems. Sorry for my english I'm from Argentina – Matias Perkins Aug 10 '18 at 12:20

1 Answers1

0

You could use a library like Apache HttpClient:

String url = "https://foourl.com"; 

TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
SSLSocketFactory sf = new SSLSocketFactory(
  acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", 8443, sf));
ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);

HttpClient client = new DefaultHttpClient(ccm);
HttpPost post = new HttpPost(url);

// set header
post.setHeader("User-Agent", "my-user-agent");
post.setHeader("Content-Type", "application/xml");

String xmlString = getXmlAsString();

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("xml", xmlString));

post.setEntity(new UrlEncodedFormEntity(urlParameters));

HttpResponse response = client.execute(post);
System.out.println("Response Code : " + 
                            response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
    result.append(line);
}

System.out.println(result.toString());

Check these links: https://www.baeldung.com/httpclient-post-http-request https://www.baeldung.com/httpclient-ssl

Bear in mind that you need to parse the xml file as a string. Check this link: https://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string

Martín Zaragoza
  • 1,523
  • 6
  • 17