0
我想提交一個表單,當我使用cookie登錄到網絡,但它似乎不工作,如何使它? (第一次嘗試獲取cookie,它工作,然後我嘗試使用cookie再次登錄併發布一些multipart/form-data,這一次不起作用,但響應代碼是:200 OK,有什麼問題?)如何使用OkHttp發佈表單時,我使用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();
}
}
你可以請張貼一些你已經試過的代碼嗎? – ib11
我的意思是,點擊你問題下面的[編輯](http://stackoverflow.com/posts/37204339/edit)鏈接,並將代碼添加到問題中,以便人們可以看到你到目前爲止所嘗試的內容。我爲你做,所以你可以看到我的意思。 – ib11