"非法国家例外:已经连接"何时使用HttpURLConnection

时间:2015-04-27 21:47:15

标签: java httpurlconnection

当我将DoOutput设置为true时,我收到非法状态异常。

public boolean sendLinksToMaster(String ipport, List<String> links){

        boolean sent = false;
        String[] tokens = ipport.split(":");    
        String data = edu.cis555.searchengine.utils.Utils.generateLinks(links);
        HttpURLConnection conn=null;
        try{
            String encodedData = URLEncoder.encode(data, "UTF-8");
        try{

                String ip =tokens[0];
                String port = tokens[1];
                String path = edu.cis555.searchengine.utils.Constants.URL_ADD_LINK;
                System.setProperty("http.keepAlive", "false");
                URL u = new URL("http", ip, Integer.parseInt(port),"/"+path);

                conn = (HttpURLConnection)u.openConnection();
                //ERROR IN THIS LINE
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
                OutputStream stream = conn.getOutputStream();
                stream.write(encodedData.getBytes());
                stream.close();

                if(conn.getResponseCode() == HttpURLConnection.HTTP_OK)
                    sent=true;

            //  LOG.debug(conn.getResponseCode());
                conn.disconnect();
            }catch(MalformedURLException mfe){
                LOG.debug(mfe.getMessage());
                if(conn!=null){
                    conn.disconnect();
                }
            }catch(IOException ioe){
                LOG.debug(ioe.getMessage());
                if(conn!=null){
                    conn.disconnect();
                }
            }

        }catch(Exception e){
            LOG.debug(e.getMessage());
            if(conn!=null){
                conn.disconnect();
            }
        }
        return sent;

    }

显示的堆栈跟踪是:

java.lang.IllegalStateException: Already connected
at java.net.URLConnection.setDoOutput(Unknown Source)
at edu.upenn.cis455.xpathengine.utils.pool.ThreadPool.sendLinksToMaster(ThreadPool.java:357)
at edu.upenn.cis455.xpathengine.utils.pool.ThreadPool$Worker.processAndAddToQueue(ThreadPool.java:314)
at edu.upenn.cis455.xpathengine.utils.pool.ThreadPool$Worker.run(ThreadPool.java:269)
at java.lang.Thread.run(Unknown Source)

发送请求时,我没有看到任何错误。任何人都可以指出缺少什么或我做错了什么

4 个答案:

答案 0 :(得分:28)

我遇到了同样的问题并解决了它。就我而言,这是因为我在NetBeans的调试界面中忘记了connection.getResponseCode()。希望它可以帮助其他人犯同样的错误。

如果您有相对于请求响应值的任何观看,例如getResponseCode()getResponseMessage()getInputStream()甚至只是connect(),您都会收到此错误消息在调试模式下。

所有以前的方法都隐式调用connect()并触发请求。因此,当您到达setDoOutput时,已建立连接。

答案 1 :(得分:0)

有时候,就像确保没有HTTP代替https一样简单。

答案 2 :(得分:0)

除了前面的评论中提到的手表以外,如果连接有问题,也可能会发生。例如:

我像post.setRequestProperty("Ocp-Apim-Subscription-Key", "<>")一样写入OutPut Stream后设置了一个属性:post.getOutputStream().write(jsonBody.getBytes("UTF-8"));

就像:

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

在这种情况下,我还获得了“已连接”的信息。要修复它,我将其设置为:

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

答案 3 :(得分:-3)

把stream.close(); 在最后一块

相关问题