“Illegal State Exception: Already Connected” when using HttpURLConnection

后端 未结 4 1704
鱼传尺愫
鱼传尺愫 2020-12-06 17:06

I get an illegal state exception when i set DoOutput to true.

public boolean sendLinksToMaster(String ipport, List links){

        boolean sen         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 17:38

    Apart from the watches as mentioned in the previous comment, it might also occur if there is something wrong with the connection. For example:

    I was setting a property:post.setRequestProperty("Ocp-Apim-Subscription-Key", "<>") after writing into OutPut Stream like post.getOutputStream().write(jsonBody.getBytes("UTF-8"));

    It was like:

    post.getOutputStream().write(jsonBody.getBytes("UTF-8"))
    post.setRequestProperty("Ocp-Apim-Subscription-Key", "<>")
    

    In this case, I was also getting "Already Connected". To fix it I made it like:

    post.setRequestProperty("Ocp-Apim-Subscription-Key", "<>")
    post.getOutputStream().write(jsonBody.getBytes("UTF-8"))
    

提交回复
热议问题