代碼如下,就是無法通過驗證,我也知道是base64的編碼,密碼也知道,可就是沒法進入,卡了兩天了
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class restartRouter {
public static void main(String[] args){
//"http://192.168.1.1/userRpm/SysRebootRpm.htm?Reboot=重啟路由器";
// 驗證的用戶名和密碼
String login_user = "admin";
String login_pw = "123456";
String auth = "Basic " + getBASE64(login_user + login_pw);
System.out.println(auth);
HttpClient httpClient = new DefaultHttpClient();
HttpUriRequest request= new HttpGet("http://192.168.1.1/userRpm/SysRebootRpm.htm?Reboot=重啟路由器");
//HttpUriRequest request= new HttpGet("http://192.168.1.1/userRpm/StatusRpm.htm?Disconnect=斷線&wan=1");
//HttpUriRequest request= new HttpGet("http://192.168.1.1/userRpm/StatusRpm.htm?Connect=連接&wan=1");
//http://192.168.1.1/userRpm/StatusRpm.htm?Disconnect=斷線&wan=1
//添加驗證信息
request.addHeader("Authorization", auth);
// 打印請求信息
try {
// 發送請求,返回響應
HttpResponse response = httpClient.execute(request);
HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(httpEntity);
// 打印響應信息
System.out.println(result);
} catch (ClientProtocolException e) {
// 協議錯誤
e.printStackTrace();
} catch (IOException e) {
// 網絡異常
e.printStackTrace();
}
}
public static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
}
不太清楚你要操作的是什麼路由器,不過思路應該不難。
1、模擬登錄,獲取登錄後的cookie.
2、帶著得到的cookie請求需要的操作。
這裡你把登錄賬號和密碼直接用base64編碼傳過去,應該是不成的。給你一個小例子:
package com.daidai.util;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupUtil {
// 登錄獲取到的cookie
static Map<String, String> cookieMap = new HashMap<String, String>();
/**
* @param uri
* @param paramsMap form表單中的參數和值
* @return
*/
public static boolean login(String uri, Map<String, String> paramsMap){
boolean loginFlag = false;
Response response = null;
try {
response = Jsoup.connect(uri)
.userAgent("Mozilla")
.data(paramsMap)
.method(Method.POST)
.timeout(20000)
.execute();
if (response.statusCode() == 200) {
cookieMap = response.cookies();
loginFlag = true;
}
} catch (IOException e) {
e.printStackTrace();
}
return loginFlag;
}
/**
* 提交評論內容
* @param url
* @param params 封裝的參數
*/
public static void doPost(String uri, Map<String,String> paramsMap){
Response response = null;
try {
response = Jsoup.connect(uri)
.userAgent("Mozilla/5.0")
.data(paramsMap)
.method(Method.POST)
.timeout(20000)
.cookies(cookieMap)
.ignoreContentType(true)
.execute();
if (response.statusCode() == 200) {
System.out.println("評論成功!");
} else{
System.out.println("評論失敗!(HTTP CODE:"+response.statusCode()+")");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
登錄操作:
System.out.println("正在進行登錄......");
Map<String, String> paramsMap = JsoupUtil.getParamsMap();
paramsMap.put("username", userName);
paramsMap.put("password", password);
boolean flag = JsoupUtil.login("**********", paramsMap);
if(flag){
System.out.println("登錄成功!");
}else{
System.out.println("登錄失敗請重新進行登錄!");
login();
}