2016-06-13 1 views
1

を接続HttpURLConnectionの不正な状態の例外は:すでに私はパラメータを掲示し、InputStreamからリダイレクトされたページを読み込むしようとしていますが、私はこの例外の何も知らない

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(); 
    } 

答えて

4

urlConnection.getInputStream()と0を呼び出す前に、urlConnection.setDoInput(true)urlConnection.setDoOutput(true)urlConnection.setRequestMethod("POST")urlConnection.setRequestMethod(Integer.toString(postDataLength))などのクエリを変更する方法を呼び出す必要があります。これらのメソッドは実際にリクエストを開始するので、一度呼び出されるとリクエストを変更するには遅すぎるので、これを得る理由はExceptionです。

レスポンス更新

あなたが実際に直面している主な問題は、あなたがあなたが、あなたがこの例外を取得する理由がある、再利用されることを意図していない状態で、TE HttpURLConnectionを再利用したいという事実によるものであり、 HTTPリクエストごとに新しいHttpURLConnectionを作成する必要があります。

+0

実際に私は実行時にパラメータを取得する必要があります。私はそれを使用しているサイトのポストparamtersには、セッションベースの暗号化があります。だから、私はそのパラメータを取得する必要がありますし、私はそれを投稿する必要があります....私はあなたが言ったことを行ったが、別の例外java.net.ProtocolExceptionを取得している:無効なHTTPメソッド:60 –

+1

"HttpURLConnection is再利用されることを意図していない "私はそれを覚えておく必要があります、ありがとう! –

+0

@Broken_Windowほとんどのクラスは再利用するためのものではありません。例外は通常はまれで、何らかの形で論理的です:ファイルハンドル、データベースハンドル、スレッド – Gewure

関連する問題