0

I am using a third party API in my program who's job is to construct a SOAP message by using the java.net.URL object passed as an input. I am constructing the URL object by passing the url as a String and thats it.

The requirement now is to attach a header to the URL before passing it to the third party API. My challenge is the API takes only URL as an input and nothing else. AS i have exhausted all my options, can you please let me know if there is any workaround or options available that can be applied in this scenario?

abhinavsinghvirsen
  • 1,853
  • 16
  • 25
Amit
  • 633
  • 6
  • 13
  • Can you post code for more details – Santosh b Feb 25 '19 at 10:34
  • @Santhosh The code looks like below. URL urlObj = new URL("http://localhost:8080"); ThirdPartyAPI obj = new ThirdPartyAPI(urlObj); now i am trying to figure our how to attach the header information to the urlObj. – Amit Feb 25 '19 at 10:37
  • I think you forgot to put the code – Santosh b Feb 25 '19 at 10:39
  • 1
    Your understanding of the URL is not right. A URL is a locater and it will never have headers. Do you mean HttpUrlConnection or SOAP message? https://en.wikipedia.org/wiki/URL – Laksitha Ranasingha Feb 25 '19 at 10:39
  • If you are trying to connect to that URL use this link to set headers https://stackoverflow.com/questions/12732422/adding-header-for-httpurlconnection Else if its a soap request you need to set the SOAPHeader – Santosh b Feb 25 '19 at 10:42
  • @Santhosh, thanks for the suggestion. Unfortunately, urlObject is the only option i have to pass it to the API.I can not construct HttpURLConnection and pass its reference to the API. – Amit Feb 25 '19 at 10:49

1 Answers1

2
URL urlObj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
connection.setRequestMethod(method.toUpperCase());
connection.setRequestProperty("Authorization", "BASIC "+new String(encodedBase64));
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setDoInput(true);
connection.setUseCaches(false);
connection.connect();

This is how I did it with my URL object
setRequestProperty will add Key-Value pair to the Header of the Request.

Kiran S
  • 78
  • 1
  • 10