简体   繁体   English

如何通过 java.net.URL 传递标头属性

[英]How to Pass header attributes via java.net.URL

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.我在我的程序中使用第三方 API,其工作是通过使用作为输入传递的 java.net.URL 对象来构造 SOAP 消息。 I am constructing the URL object by passing the url as a String and thats it.我通过将 url 作为字符串传递来构建 URL 对象,就是这样。

The requirement now is to attach a header to the URL before passing it to the third party API.现在的要求是在将 URL 传递给第三方 API 之前将标头附加到 URL。 My challenge is the API takes only URL as an input and nothing else.我的挑战是 API 仅将 URL 作为输入,而没有其他任何内容。 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?因为我已经用尽了我所有的选择,你能告诉我是否有任何可用的解决方法或选项可以应用于这种情况吗?

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这就是我使用 URL 对象的方式
setRequestProperty will add Key-Value pair to the Header of the Request. setRequestProperty将键值对添加到请求的头部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM