set 方法不适用于 HttpsURLConnection

发布于 2024-12-05 21:50:53 字数 1493 浏览 0 评论 0原文

我正在尝试用 java 发出休息请求。我在 Firefox 中使用 RestClient 测试了 Web 服务,效果很好。

当我尝试修改 java 中的 HttpsUrlConnection 实例时,值没有改变,并且我收到 500 响应代码。

这是我的代码:

    public  String getAuthToken() throws IOException {
    URL url =new URL("https://webserviceurl"); // webservice url is the url of the webservice
    String data = URLEncoder.encode("username") + "=" + URLEncoder.encode("myusername","UTF-8");
    data+= "&" + URLEncoder.encode("password") + "=" + URLEncoder.encode("pass","UTF-8");
    HttpsURLConnection conn =(HttpsURLConnection) url.openConnection();
    conn.setUseCaches(false);
    conn.setHostnameVerifier(new AllowAllHostnameVerifier()); //this works HostName verifier changes
    conn.setRequestMethod("POST"); // this doens't work. requestMethod is still set to GET
    conn.setDoOutput(true); // this doesnt work. DoOutput still set on false
    conn.setRequestProperty("Content-Type", "application/json"); // doens't work either
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
    wr.write(data);
    wr.flush();
    wr.close();
     //conn has a 500 response code
    if (conn.getResponseCode()==200)
    {
    InputStream is = conn.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader rd = new BufferedReader(isr);
    String token = rd.readLine();
    rd.close();
    return token;
    }
    else
        return null;

}

我被困在这一点上,找不到任何东西来完成这项工作。

谢谢你!

I'm trying to make a rest request in java. I tested the web service using RestClient in Firefox and it works great.

When i try to modify the HttpsUrlConnection instance in java the values aren't changing and i get a 500 response code.

Here's my code:

    public  String getAuthToken() throws IOException {
    URL url =new URL("https://webserviceurl"); // webservice url is the url of the webservice
    String data = URLEncoder.encode("username") + "=" + URLEncoder.encode("myusername","UTF-8");
    data+= "&" + URLEncoder.encode("password") + "=" + URLEncoder.encode("pass","UTF-8");
    HttpsURLConnection conn =(HttpsURLConnection) url.openConnection();
    conn.setUseCaches(false);
    conn.setHostnameVerifier(new AllowAllHostnameVerifier()); //this works HostName verifier changes
    conn.setRequestMethod("POST"); // this doens't work. requestMethod is still set to GET
    conn.setDoOutput(true); // this doesnt work. DoOutput still set on false
    conn.setRequestProperty("Content-Type", "application/json"); // doens't work either
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
    wr.write(data);
    wr.flush();
    wr.close();
     //conn has a 500 response code
    if (conn.getResponseCode()==200)
    {
    InputStream is = conn.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader rd = new BufferedReader(isr);
    String token = rd.readLine();
    rd.close();
    return token;
    }
    else
        return null;

}

I'm stucked at this point and cannot find anything to make this work.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2024-12-12 21:50:53

我实际上认为这是 HttpsURLConnection 的一个错误。当我将其更改为 HttpURLConnection 对象时,一切正常。

I actually think it's a bug with HttpsURLConnection. As i changed it to a HttpURLConnection object everything works just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文