??? /**
???? * 方法名稱:getPostPage<p>
???? * 方法功能:自動登錄請求頁面<p>
???? * 參數說明:登陸主機的ip地址, 端口號<p>
???? * 返回:<p>
???? * 作者:李明
???? * 日期:2006年3月13日
???? */
??? public void getPostPage(String host, int port)
??? {
??????? HttpClient httpClient = new HttpClient();
??????? httpClient.getHostConfiguration().setHost(host, port);
??????? // 創建POST方法的實例
??????? PostMethod postMethod = new PostMethod(url);
??????? // 使用系統提供的默認的恢復策略
??????? postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
??????? // 填入各個表單域的值
??????? NameValuePair[] data =
??????????? {new NameValuePair("tUsername", "admin"), new NameValuePair("tPassword", "admin")};
??????? // 將表單的值放入postMethod中
??????? postMethod.setRequestBody(data);
??????? try
??????? {
??????????? // 執行postMethod
??????????? int statusCode = httpClient.executeMethod(postMethod);
??????????? System.out.println("statusCode " + statusCode);
??????????? // HttpClient對于要求接受后繼服務的請求,象POST和PUT等不能自動處理轉發
??????????? // 301或者302
??????????? if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
??????????? {
??????????????? // 從頭中取出轉向的地址
??????????????? Header locationHeader = postMethod.getResponseHeader("location");
??????????????? String location = null;
??????????????? if(locationHeader != null)
??????????????? {
??????????????????? location = locationHeader.getValue();
??????????????????? System.out.println("The page was redirected to:" + location);
??????????????? }
??????????????? else
??????????????? {
??????????????????? System.err.println("Location field value is null.");
??????????????? }
??????????????? return;
??????????? }
??????????? if(statusCode != HttpStatus.SC_OK)
??????????? {
??????????????? System.err.println("Method failed: " + postMethod.getStatusLine());
??????????? }
??????????? System.out.println(statusCode);
??????????? /*
???????????? * //如果所請求的頁面已設定cookie那么無需返回內容,可直接使用url訪問登陸成功頁面。
???????????? * // 讀取內容
???????????? *? byte[] responseBody = postMethod.getResponseBody();
???????????? * // 處理內容
???????????? *? System.out.println(new String(responseBody));
???????????? */
??????? }
??????? catch(HttpException e)
??????? {
??????????? // 發生致命的異常,可能是協議不對或者返回的內容有問題
??????????? System.out.println("Please check your provided http address!");
??????????? e.printStackTrace();
??????? }
??????? catch(IOException e)
??????? {
??????????? // 發生網絡異常
??????????? e.printStackTrace();
??????? }
??????? finally
??????? {
??????????? // 釋放連接
??????????? postMethod.releaseConnection();
??????? }
??? }
??? /**
???? * 方法名稱:getPostPage163<p>
???? * 方法功能:返回163信箱自動登陸頁面<p>
???? * 參數說明:163郵箱用戶名和密碼<p>
???? * 返回:String 登陸成功后的頁面<p>
???? * 作者:李明
???? * 日期:2006年3月13日
???? */
??? public static String getPostPage163(String userid, String passid)
??? {
??????? HttpClient httpClient = new HttpClient();
??????? httpClient.getHostConfiguration().setProxy("proxynj.zte.com.cn", 80);
??????? String url = "??????? String buff = null;
??????? // 創建POST方法的實例
??????? PostMethod postMethod = new PostMethod(url);
??????? // 使用系統提供的默認的恢復策略
??????? postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
??????? // 填入各個表單域的值
??????? NameValuePair[] data =
??????????? {new NameValuePair("username", userid), new NameValuePair("password", passid)};
??????? // 將表單的值放入postMethod中
??????? postMethod.setRequestBody(data);
??????? try
??????? {
??????????? // 執行postMethod
??????????? int statusCode = httpClient.executeMethod(postMethod);
??????????? // HttpClient對于要求接受后繼服務的請求,象POST和PUT等不能自動處理轉發
??????????? // 301或者302
??????????? if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
??????????? {
??????????????? // 從頭中取出轉向的地址
??????????????? Header locationHeader = postMethod.getResponseHeader("location");
??????????????? String location = null;
??????????????? if(locationHeader != null)
??????????????? {
??????????????????? location = locationHeader.getValue();
??????????????????? System.out.println("The page was redirected to:" + location);
??????????????? }
??????????????? else
??????????????? {
??????????????????? System.err.println("Location field value is null.");
??????????????? }
??????????????? return null;
??????????? }
??????????? if(statusCode != HttpStatus.SC_OK)
??????????? {
??????????????? System.err.println("Method failed: " + postMethod.getStatusLine());
??????????? }
??????????? // 讀取內容
??????????? byte[] responseBody = postMethod.getResponseBody();
??????????? // 處理內容
??????????? System.out.println(new String(responseBody));
??????????? String redirect = new String(responseBody);
??????????? System.out.println(redirect.lastIndexOf("></HEAD>"));
??????????? System.out.println(redirect.substring(redirect.indexOf("URL") + 4, redirect.lastIndexOf("></HEAD>") - 1));
??????????? redirect = redirect.substring(redirect.indexOf("URL") + 4, redirect.lastIndexOf("></HEAD>") - 1);
??????????? GetMethod getMethod = new GetMethod("??????????? httpClient.executeMethod(getMethod);
??????????? buff = getMethod.getResponseBodyAsString();
??????? }
??????? catch(HttpException e)
??????? {
??????????? // 發生致命的異常,可能是協議不對或者返回的內容有問題
??????????? System.out.println("Please check your provided http address!");
??????????? e.printStackTrace();
??????? }
??????? catch(IOException e)
??????? {
??????????? // 發生網絡異常
??????????? e.printStackTrace();
??????? }
??????? finally
??????? {
??????????? // 釋放連接
??????????? postMethod.releaseConnection();
??????? }
??????? return buff;
??? }