httpurlconnection非法状态异常:已连接

时间:2016-06-13 17:26:14

标签: java httpurlconnection

我正在尝试发布参数并从输入流中读取重定向页面,但我不知道此异常的任何内容

Exception in thread "main" java.lang.IllegalStateException: Already connected
    at java.net.URLConnection.setDoOutput(URLConnection.java:900)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.setDoOutput(HttpsURLConnectionImpl.java:455)
    at com.company.Main.main(Main.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

代码:

try {

        CookieHandler cookieHandler = null;
        CookieManager.setDefault(cookieHandler);


        HttpURLConnection urlConnection = (HttpURLConnection) new URL("site").openConnection();
        InputStream i = new BufferedInputStream(urlConnection.getInputStream());
        StringBuilder stringBuilder =new StringBuilder();


        int c;
        while ((c=i.read())!=-1){
            stringBuilder.append((char)c);
        }

         // for getting the post paramters that is user name and password field
        Pattern p = Pattern.compile("<input name=\"\\w{22}\" type=\"text\" id=\"\\w{22}\" />");
        Matcher matcher = p.matcher(stringBuilder.toString());

        String user_name = null;
        while (matcher.find()){

            user_name = matcher.group().split("\"")[1];

        }

        String password = null;
        p= Pattern.compile("<input name=\"\\w{22}\" type=\"password\" id=\"\\w{22}\" />");
        matcher= p.matcher(stringBuilder.toString());
        while (matcher.find()){

            password = matcher.group().split("\"")[1];

        }
        System.out.print(user_name + "  "  + password);



        String urlParameters  = user_name+"=xyz&"+password+"=xyz";
        byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
        int    postDataLength = postData.length;

        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestMethod(Integer.toString(postDataLength));


        Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));
        writer.write(urlParameters);
        writer.flush();

        urlConnection.setDoInput(true);
        urlConnection.getContent();

        while ((c=i.read())!=-1){
            System.out.print((char)c);

        }


    } catch (IOException e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:5)

在调用urlConnection.setDoInput(true)和{{1}之前,您需要致电urlConnection.setDoOutput(true)urlConnection.setRequestMethod("POST")urlConnection.setRequestMethod(Integer.toString(postDataLength))urlConnection.getInputStream()以及更改查询的任何其他方法因为这些方法实际上会启动请求所以一旦调用它就太晚了,无法修改请求,这就是你得到这个urlConnection.getOutputStream()的原因。

响应更新

您实际面临的主要问题是由于您希望重用Exception而不打算重复使用,这就是您获得此异常的原因,您需要创建一个新的每个HTTP请求HttpURLConnection