Using java class HttpsURLConnection

前端 未结 9 1434
孤独总比滥情好
孤独总比滥情好 2020-12-14 00:41

I have a small piece of code which basically impements a HTTP-Client, i.e. it POSTS request and works with re RESPONSE. As long as HTTP is concenerned everthing work well. F

相关标签:
9条回答
  • 2020-12-14 01:02

    Your url's protocol should also be https and not http. Check your url.

    0 讨论(0)
  • 2020-12-14 01:14

    Check your imports, you should be using

    java.net.HttpURLConnection
    

    or

    javax.net.ssl.HttpsURLConnection
    
    0 讨论(0)
  • 2020-12-14 01:15

    Just keep it java.net.URLConnection or cast it to java.net.HttpURLConnection instead. Both offers methods to do the desired task as good.


    A side remark unrelated to the technical problem: you should never explicitly import/use Sun Java SE implementation specific classes in your code. Those are undocumented classes and are subject to changes which may cause your code break when you upgrade the JVM. On the other hand, your code may also break when you run it at a different brand JVM.


    Update: since you seem to accidentally have imported it, go to Window > Preferences > Java > Appearance > Type Filters and Add com.sun.* and sun.* to the list. This way you won't ever import them accidentally:

    0 讨论(0)
  • 2020-12-14 01:16

    Check value of your "serverAddress" variable. It should https and not http

    0 讨论(0)
  • 2020-12-14 01:16

    Hard to tell without seeing the whole file, but it looks like you're importing com.sun.net.ssl.HttpsURLConnection when you really want javax.net.ssl.HttpsURLConnection.

    0 讨论(0)
  • 2020-12-14 01:16

    In my case, the protocol and port were not correct while invoking the httpsUrlConnection.

    Port and protocol were defined as static class variables. And the step prior to the failed step, was invoking an httpUrlConnection. That method changed the port/protocol to 80/http, but didn't set it back to /https at the end. So eventhough httpsUrlConnection was invoked, it was still using http/80. Once I reset those at the end of the httpUrlConnection method, the error disappeared.

    0 讨论(0)
提交回复
热议问题