HttpUrlConnection addRequestProperty方法未传递参数

编程入门 行业动态 更新时间:2024-10-25 18:28:34
本文介绍了HttpUrlConnection addRequestProperty方法未传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些可以执行以下操作的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方法未传递参数

本文发布于:2023-10-12 00:13:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   方法   HttpUrlConnection   addRequestProperty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!