Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

httpurlconnection illegal state exception : already connected

I am trying to post parameters and read the redirected page from inputstream but i don't know anything of this exception

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)

Code:

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();
    }
like image 577
Shailab Singh Avatar asked Oct 12 '25 02:10

Shailab Singh


1 Answers

You need to call urlConnection.setDoInput(true), urlConnection.setDoOutput(true), urlConnection.setRequestMethod("POST"), urlConnection.setRequestMethod(Integer.toString(postDataLength)) and any other methods that change the query before calling urlConnection.getInputStream() and urlConnection.getOutputStream() as those methods will actually launch the request so once called it is too late to modify the request that is the reason why you get this Exception.

Response Update

The main problem that you are actually facing is due to the fact that you want to reuse the HttpURLConnection while it is not meant to be reused, that is why you get this exception, you need to create a new HttpURLConnection for each HTTP request.

like image 179
Nicolas Filotto Avatar answered Oct 14 '25 15:10

Nicolas Filotto