level 6
vyzbzz
楼主
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class Demodenglu2 {
public static void login(String username,String password,String message) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient(); //实例化一个HttpClient
HttpResponse response = null;
HttpEntity entity = null;
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); //设置cookie的兼容性
HttpPost httppost = new HttpPost("http://v.m.zjxuexi.com/a/forum.php?mod=viewthread&tid=21&extra=page%3D1"); //引号中的参数是:servlet的地址
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", username));
nvps.add(new BasicNameValuePair("mod", "viewthread"));
// BasicNameValuePair("name", "value"), name是post方法里的属性, value是传入的参数值
nvps.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); //将参数传入post方法中
response = httpclient.execute(httppost); //执行
entity = response.getEntity(); //返回服务器响应
String Newcookie = response.getFirstHeader("Set-Cookie").getValue();
System.out.println("cookie="+Newcookie.substring(0,Newcookie.indexOf(";")));
try{
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine()); //服务器返回状态
Header[] headers = response.getAllHeaders(); //返回的HTTP头信息
for (int i=0; i<headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
String responseString = null;
if (response.getEntity() != null) {
responseString = EntityUtils.toString(response.getEntity()); /// /返回服务器响应的HTML代码
System.out.println("登陆成功"); //打印出服务器响应的HTML代码
}
else
{
System.out.println("登录失败");
}
}
finally {
if (entity != null)
entity.consumeContent(); // release connection gracefully
//发贴
}
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
HttpPost httppost2 = new HttpPost("http://v.m.zjxuexi.com/a/forum.php?mod=post&action=reply&fid=41&tid=21&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1");
List <NameValuePair> send = new ArrayList <NameValuePair>();
send.add(new BasicNameValuePair("message", message));
// send.add(new BasicNameValuePair("", ""));
send.add(new BasicNameValuePair("handlekey", "fastpost"));
send.add(new BasicNameValuePair("Referer", "http://v.m.zjxuexi.com/a/forum.php?mod=viewthread&tid=21&extra=page%3D1&page=1"));
send.add(new BasicNameValuePair("username", username));
send.add(new BasicNameValuePair("password", password));
send.add(new BasicNameValuePair("usesig", "1"));
send.add(new BasicNameValuePair("topicsubmit", "yes"));
send.add(new BasicNameValuePair("infloat", "yes"));
send.add(new BasicNameValuePair("action", "newthread"));
send.add(new BasicNameValuePair("mod", "post"));
send.add(new BasicNameValuePair("cookie", Newcookie));
httppost.setEntity(new UrlEncodedFormEntity(send, HTTP.UTF_8)); //将参数传入post方法中
response = httpclient.execute(httppost2);
try{
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine()); //服务器返回状态
Header[] headers = response.getAllHeaders(); //返回的HTTP头信息
for (int i=0; i<headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
String responseString = null;
if (response.getEntity() != null) {
responseString = EntityUtils.toString(response.getEntity()); /// /返回服务器响应的HTML代码
System.out.println("发贴成功"); //打印出服务器响应的HTML代码
}
else
{
System.out.println("发贴失败");
}
}
finally {
if (entity != null)
entity.consumeContent(); // release connection gracefully
//发贴
}
}
public static void main(String[] args) throws Exception
{
login("Forrenew", "wzking007","坏人");
}
}
2015年11月09日 06点11分
1
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class Demodenglu2 {
public static void login(String username,String password,String message) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient(); //实例化一个HttpClient
HttpResponse response = null;
HttpEntity entity = null;
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); //设置cookie的兼容性
HttpPost httppost = new HttpPost("http://v.m.zjxuexi.com/a/forum.php?mod=viewthread&tid=21&extra=page%3D1"); //引号中的参数是:servlet的地址
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", username));
nvps.add(new BasicNameValuePair("mod", "viewthread"));
// BasicNameValuePair("name", "value"), name是post方法里的属性, value是传入的参数值
nvps.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); //将参数传入post方法中
response = httpclient.execute(httppost); //执行
entity = response.getEntity(); //返回服务器响应
String Newcookie = response.getFirstHeader("Set-Cookie").getValue();
System.out.println("cookie="+Newcookie.substring(0,Newcookie.indexOf(";")));
try{
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine()); //服务器返回状态
Header[] headers = response.getAllHeaders(); //返回的HTTP头信息
for (int i=0; i<headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
String responseString = null;
if (response.getEntity() != null) {
responseString = EntityUtils.toString(response.getEntity()); /// /返回服务器响应的HTML代码
System.out.println("登陆成功"); //打印出服务器响应的HTML代码
}
else
{
System.out.println("登录失败");
}
}
finally {
if (entity != null)
entity.consumeContent(); // release connection gracefully
//发贴
}
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
HttpPost httppost2 = new HttpPost("http://v.m.zjxuexi.com/a/forum.php?mod=post&action=reply&fid=41&tid=21&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1");
List <NameValuePair> send = new ArrayList <NameValuePair>();
send.add(new BasicNameValuePair("message", message));
// send.add(new BasicNameValuePair("", ""));
send.add(new BasicNameValuePair("handlekey", "fastpost"));
send.add(new BasicNameValuePair("Referer", "http://v.m.zjxuexi.com/a/forum.php?mod=viewthread&tid=21&extra=page%3D1&page=1"));
send.add(new BasicNameValuePair("username", username));
send.add(new BasicNameValuePair("password", password));
send.add(new BasicNameValuePair("usesig", "1"));
send.add(new BasicNameValuePair("topicsubmit", "yes"));
send.add(new BasicNameValuePair("infloat", "yes"));
send.add(new BasicNameValuePair("action", "newthread"));
send.add(new BasicNameValuePair("mod", "post"));
send.add(new BasicNameValuePair("cookie", Newcookie));
httppost.setEntity(new UrlEncodedFormEntity(send, HTTP.UTF_8)); //将参数传入post方法中
response = httpclient.execute(httppost2);
try{
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine()); //服务器返回状态
Header[] headers = response.getAllHeaders(); //返回的HTTP头信息
for (int i=0; i<headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
String responseString = null;
if (response.getEntity() != null) {
responseString = EntityUtils.toString(response.getEntity()); /// /返回服务器响应的HTML代码
System.out.println("发贴成功"); //打印出服务器响应的HTML代码
}
else
{
System.out.println("发贴失败");
}
}
finally {
if (entity != null)
entity.consumeContent(); // release connection gracefully
//发贴
}
}
public static void main(String[] args) throws Exception
{
login("Forrenew", "wzking007","坏人");
}
}