2016-05-13 1 views
0

私はウェブにクッキーのログインを使用するときにフォームを提出したいが、それはうまくいかない、それを作る方法? (初めてクッキーを取得しようとすると、クッキーを使って再度ログインし、マルチパート/フォームデータを投稿しようとしましたが、今回はうまくいかず、レスポンスコードは:200 OK、何が問題なの?)OkHttpを使用してWebにCookieログインを使用するときにフォームを投稿するには?

public static void initRequestBody() { 
     //this is the requested form data 
     requestBody = new MultipartBuilder() 
       .type(MultipartBuilder.FORM) 
       .addPart(Headers.of("Content-Disposition", "form-data; name=\"cbid.network.wan.proto\""), 
         RequestBody.create(null, "pppoe")) 
       .addPart(Headers.of("Content-Disposition", "form-data; name=\"cbid.network.wan.username\""), 
         RequestBody.create(null, "[email protected]")) 
       .addPart(Headers.of("Content-Disposition", "form-data; name=\"cbid.network.wan.password\""), 
         RequestBody.create(null, "85603")) 
       .addPart(Headers.of("Content-Disposition", "form-data; name=\"cbid.network.wan.dialtype\""), 
         RequestBody.create(null, "3")) 
       .addPart(Headers.of("Content-Disposition", "form-data; name=\"cbid.network.wan.macaddr\""), 
         RequestBody.create(null, "")) 
       .addPart(Headers.of("Content-Disposition", "form-data; name=\"cbid.network.wan.step\""), 
         RequestBody.create(null, "4")) 
       .addPart(Headers.of("Content-Disposition", "form-data; name=\"cbid.network.wan.quit\""), 
         RequestBody.create(null, "0")) 
       .addPart(Headers.of("Content-Disposition", "form-data; name=\"cbid.network.wan.mytime\""), 
         RequestBody.create(null, "0")) 
       .build(); 
    } 
    //try to get cookie 
    public static void parseWebInit() { 
     //try login 
     client = new OkHttpClient(); 
     //data 
     RequestBody haha = new FormEncodingBuilder() 
       .add("username1", "root") 
       .add("password1", "admin") 
       .build(); 

     //get cookie 
     Request request = new Request.Builder() 
       .url("http://192.168.1.1/cgi-bin/luci/") 
       .header("Connection", "keep-alive") 
       .addHeader("Content-Type", "application/x-www-form-urlencoded") 
       .addHeader("User-Agent", userAgent) 
       .post(haha) 
       .build(); 
     try { 
      Response response = client.newCall(request).execute(); 
      cookie = response.header("Set-Cookie"); 
      stoke = cookie.split(";")[2];//this will insert into the new url 
      cookie = cookie.split(";")[0];//I will use it 
      System.out.println(cookie); 
      System.out.println(stoke); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public static void parseWebAgain() { 
     //use cookie to login 

     Request request = new Request.Builder() 
       .url("http://192.168.1.1/cgi-bin/luci/;" + stoke + "/admin/guide/") 
       .header("Connection", "keep-alive") 
       .addHeader("Content-Type","multipart/form-data; boundary=----WebKitFormBoundaryy5MZpl0mcqPZCYZT") 
       .addHeader("User-Agent", userAgent) 
       .addHeader("Cookie", cookie) 
       .post(requestBody) 
       .build(); 

     Response response = client.newCall(request).execute(); 
     try { 

      InputStream instream = response.body().byteStream(); 
      int l = 0; 
      byte[] temp = new byte[2048]; 
      while ((l = instream.read(temp)) != -1) { 
       System.out.println(new String(temp, 0, l, "UTF-8")); 
      } 
      System.out.println(response.headers()); 
      System.out.println(response.code()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
+0

既に試したコードを投稿してください。 – ib11

+0

質問のすぐ下にある[編集](http://stackoverflow.com/posts/37204339/edit)のリンクをクリックして質問にコードを追加すると、これまでに試したことを人々が見ることができます。私はあなたのためにそれをするので、あなたは私が何を意味するか見ることができます。 – ib11

答えて

0

このようなクッキーを手動で取得したくない場合は、 cookieJarの使用を強くお勧めします。

/* 
* Creates a new client from the global client with 
* a stateful cookie jar. This is useful when you need 
* to access password protected sites. 
*/ 
public static OkHttpClient newClientWithCookieJar() { 
    CookieManager cookieManager = new CookieManager(); 
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); 
    JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager); 
    return client.newBuilder().cookieJar(cookieJar).build(); 
} 

これは、自動的にクッキーを解析し、同じクライアントを使用している限りすべてのリクエストに追加します。

関連する問題