?? httptest1.java~122~
字號(hào):
package httpclient;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.*;
public class HttpTest1 {
public HttpTest1() {
}
public static void main(String[] args) {
HttpTest1 httpTest = new HttpTest1();
String urlStr = httpTest.test1();
for (int i = 0; i < 30; i++) {
try {
System.out.println("********************************");
urlStr = httpTest.test2(urlStr);
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}
public String test1() {
String retStr = "";
try {
HttpClient client = new HttpClient();
GetMethod get = new GetMethod("http://localhost/first.jsp?r=494");
// UsernamePasswordCredentials upc =
// new UsernamePasswordCredentials("foo", "bar");
//client.getState().setCredentials(null, null, upc);
// get.setDoAuthentication(true);
//client.setConnectionTimeout(60000);
client.executeMethod(get);
String str = get.getResponseBodyAsString();
System.out.println(str.substring(str.indexOf("<card id")).substring(
50, 100));
int l1 = str.indexOf("href=");
str = str.substring(l1 + "href=".length() + 1);
l1 = str.indexOf("href=");
str = str.substring(l1 + "href=".length() + 1);
//System.out.println("str1 is: " + str);
int l2 = str.indexOf(">");
str = str.substring(0, l2 - 1);
retStr = "http://localhost/" + str;
System.out.println("" + retStr);
get.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
return retStr;
}
public String test2(String argStr) {
String urlStr = "";
try {
HttpClient client = new HttpClient();
GetMethod get = new GetMethod("http://localhost/first.jsp?r=494");
// UsernamePasswordCredentials upc =
// new UsernamePasswordCredentials("foo", "bar");
//client.getState().setCredentials(null, null, upc);
// get.setDoAuthentication(true);
//client.setConnectionTimeout(60000);
GetMethod redirect = new GetMethod(argStr);
client.executeMethod(redirect);
String str = redirect.getResponseBodyAsString();
String str2 = str;
//System.out.println("str: \n" + str);
int l1 = str.indexOf("href=");
str = str.substring(l1 + "href=".length() + 1);
l1 = str.indexOf("href=");
str = str.substring(l1 + "href=".length() + 1);
//System.out.println("str1 is: " + str);
int l2 = str.indexOf(">");
str = str.substring(0, l2 - 1);
urlStr = "http://localhost/" + str;
System.out.println("" + urlStr);
System.out.println(str2.substring(str2.indexOf("<card id")).
substring(
50, 100));
System.out.println("Redirect: " + redirect.getStatusLine().toString());
// release any connection resources used by the method
redirect.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
return urlStr;
}
public void postTest(String argStr) {
HttpClient client = new HttpClient();
PostMethod authpost = new PostMethod("argStr");
NameValuePair action = new NameValuePair("User-Agent", "xiaoxj user agent");
authpost.setRequestBody(
new NameValuePair[] {action});
client.executeMethod(authpost);
System.out.println("Login form post: " + authpost.getStatusLine().toString());
// release any connection resources used by the method
authpost.releaseConnection();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -