当我使用cookie登录到网络时,如何使用OkHttp发布表单?

时间:2016-05-13 07:54:16

标签: java post okhttp

我想在使用cookie登录网页时提交表单,但它似乎不起作用,如何制作? (我第一次尝试获取cookie,它工作,然后我尝试使用cookie再次登录,并发布一些multipart / form-data,这次没有工作,但响应代码是:200好的,问题是什么?)

    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, "18030844937@96301"))
                .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();
        }

    }

1 个答案:

答案 0 :(得分:0)

你不想手动抓取那样的cookie。我强烈建议使用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();
}

只要您使用同一个客户端,这将自动解析cookie并将其添加到每个请求中。