作者:炎木柏玮 | 来源:互联网 | 2023-05-24 18:52
packagecore;importjava.io.BufferedReader;importjava.io.InputStreamReader;importorg.apache.com
package core;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.COOKIE;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.COOKIE.COOKIEPolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class ClientCore {
public static void main(String[] args) {
String url = "http://login.taobao.com/member/login.jhtml";// 论坛的登陆页面
String url2 = "http://bbs.taobao.com/catalog/thread/154521-256344700.htm?action=/forum/reply_action";// 论坛的发贴页面
HttpClient httpClient = new HttpClient();
httpClient.getParams().setCOOKIEPolicy(
COOKIEPolicy.BROWSER_COMPATIBILITY);
PostMethod postMethod = new PostMethod(url);
PostMethod postMethod2 = new PostMethod(url2);
NameValuePair[] data = {
new NameValuePair("TPL_username", "w627782664"),
new NameValuePair("TPL_password", "wangjianxu0808"),
new NameValuePair("action", "Authenticator"),
new NameValuePair("event_submit_do_login", "anything"),
new NameValuePair("TPL_redirect_url", "http://bbs.taobao.com/"),
};
postMethod.setRequestHeader("Referer",
"http://discuzdemo.c88.53dns.com/index.php");
postMethod.setRequestHeader("Host", "discuzdemo.c88.53dns.com");
postMethod
.setRequestHeader(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2");
postMethod
.setRequestHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
postMethod.setRequestBody(data);
try {
httpClient.executeMethod(postMethod);
StringBuffer respOnse= new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
postMethod.getResponseBodyAsStream(), "gb2312"));// 以gb2312编码方式打印从服务器端返回的请求
String line;
while ((line = reader.readLine()) != null) {
response.append(line).append(
System.getProperty("line.separator"));
}
reader.close();
Header header = postMethod.getResponseHeader("Set-COOKIE");
COOKIE[] COOKIEs = httpClient.getState().getCOOKIEs();// 取出登陆成功后,服务器返回的COOKIEs信息,里面保存了服务器端给的“临时证”
String tmpCOOKIEs = "";
for (COOKIE c : COOKIEs) {
tmpCOOKIEs = tmpCOOKIEs + c.toString() + ";";
System.out.println(c);
}
System.out.println(tmpCOOKIEs);
// System.out.println(header.getValue());
System.out.println(response);
NameValuePair[] data2 = { new NameValuePair("_fmw.publis._0.g", "154521"),
new NameValuePair("_fmw.publis._0.t", "255847173"),
new NameValuePair("event_submit_do_publish", "anything"),
new NameValuePair("_fmw.publis._0.c", "测试123测试123测试123测试123测试123测试123测试123测试123测试123测试123测试123测试123")
};
postMethod2.setRequestHeader("COOKIE", tmpCOOKIEs);// 将“临时证明”放入下一次的发贴请求操作中
postMethod2.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");// 因为发贴时候有中文,设置一下请求编码
postMethod2.setRequestHeader("Referer",
"http://discuzdemo.c88.53dns.com/forumdisplay.php?fid=4");
postMethod2.setRequestHeader("Host", "discuzdemo.c88.53dns.com");
postMethod2
.setRequestHeader(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2");
postMethod2
.setRequestHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");// 以上操作是模拟浏览器的操作,使用服务器混淆
postMethod2.setRequestBody(data2);
httpClient.executeMethod(postMethod2);
StringBuffer response1 = new StringBuffer();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(
postMethod2.getResponseBodyAsStream(), "gb2312"));
String line1;
while ((line1 = reader1.readLine()) != null) {
response1.append(line1).append(
System.getProperty("line.separator"));
}
reader1.close();
System.out.println(response1);
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
} finally {
postMethod.releaseConnection();
postMethod2.releaseConnection();
}
}
}