0
我试图通过使用HttpURLConnection的http url发送XML。使用HttpURLConnection的http上的XML - 获取错误401
以下是我用来测试服务的测试卷曲命令。
curl -H "Content-Type: text/xml" -H "User-Agent: some/7.88/1" -X POST --Basic -u "username:password" -d '< ?xml packet ... >' http://ip:port/some/url
它工作正常,但是当我尝试使用下面的Java代码发送这样的:
Java代码:
URL url = new URL("http://ip:port/some/url");
String requestXMLPacket = "<?xml packet ... >";
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","text/xml");
conn.setRequestProperty("User-Agent", "some/7.88/1");
conn.setRequestProperty("basic -u", "username:password");
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setUseCaches(false);
conn.setDoInput(true);
我得到一个错误:
java.io.IOException: Server returned HTTP response code: 401 for URL: http://ip:port/some/url
这意味着我没有发送正确的授权。
你能告诉我我在做什么错在这里。
非常感谢... –
对于Java 7使用: ** org.apache.tomcat.util.codec.binary.Base64.encodeBase64String ** –