我有一些可以执行以下操作的Java代码:
I have some working java code which does the following:
URL myUrl = new URL("localhost:8080/webservice?user=" + username + "&password=" + password + "&request=x"); HttpURLConnection myConnection = (HttpURLConnection) myUrl.openConnection(); myConnection.setRequestMethod("POST"); // code continues to read the response stream但是,我注意到我的Web服务器访问日志包含了所有连接用户的明文密码.我想从访问日志中删除它,但是Web服务器管理员声称这需要在我的代码中进行更改,而不是通过Web服务器配置进行更改.
However, I noticed that my webserver access log contained the plaintext password for all of the users who connected. I would like to get this out of the access log, but the webserver admins claim that this needs to be changed in my code and not via webserver config.
我尝试将代码更改为以下内容:
I tried changing the code to the following:
URL myUrl = new URL("localhost:8080/webservice"); HttpURLConnection myConnection = (HttpURLConnection) myUrl.openConnection(); myConnection.setRequestMethod("POST"); // start of new code myConnection.setDoOutput(true); myConnection.addRequestProperty("username", username); myConnection.addRequestProperty("password", password); myConnection.addRequestProperty("request", "x"); // code continues to read the response stream现在,访问日志中不包含用户名/密码/请求方法.但是,该Web服务现在会引发异常,表明它没有收到任何用户名/密码.
Now the access log does not contain the username/password/request method. However, the webservice now throws an exception indicating that it didn't receive any username/password.
我的客户代码有什么错?我还尝试使用"setRequestProperty"代替"addRequestProperty",它具有相同的损坏行为.
What did I do wrong in my client code? I also tried using "setRequestProperty" instead of "addRequestProperty" and it had the same broken behavior.
推荐答案我实际上在另一个关于stackoverflow的问题中找到了答案.
正确的代码应为:
URL myUrl = new URL("localhost:8080/webservice"); HttpURLConnection myConnection = (HttpURLConnection) myUrl.openConnection(); myConnection.setRequestMethod("POST"); myConnection.setDoOutput(true); DataOutputStream wr = new DataOutputStream(myConnection.getOutputStream ()); wr.writeBytes("username=" + username + "&password="+password + "&request=x"); // code continues to read the response stream更多推荐
HttpUrlConnection addRequestProperty方法未传递参数
发布评论