我有一个我传入的 URL,看起来像这样
http://somecompany.com/restws/ebi/SVI/4048/?Name=Tra&Brand=Software: WebSphere - Open App Servers
它不喜欢第二个参数(品牌)。在浏览器中,上面的这个查询字符串工作正常,但一旦我在 Java 中执行,它就会失败。当我更改 web 服务以接受单个参数时,此 URL 工作正常
http://somecompany.com/restws/ebi/SVI/4048/?Name=Tra
看来 java 的第二个参数有问题。我已经尝试过转义字符和我能想到的所有其他方法,但似乎没有任何效果。请帮忙!
String uri = "somecompany.com/restws/ebi/SVI/4048/?Name="
+ name+ "&Brand=Software: WebSphere - Open App Servers";
URL url;
try {
url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
}
...
最佳答案
尝试url编码你的参数。
像这样:
String uri = "somecompany.com/restws/ebi/SVI/4048/?Name=" +name+ "&Brand=";
uri = URLEncoder.encode("Software: WebSphere - Open App Servers", "utf-8");
关于java - 带参数的 HttpURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11742288/