所以我的目标是使用httpclient登录论坛,然后在该论坛上发布回复帖子。我登录很好,但是当我在帖子上发帖时它说我没有登录。有什么想法吗?我已经尝试使用cookie构建httpClient,但是在我登录后我查看了它们并且没有任何内容。所以我尝试使用相同的httpclient进行两次调用,但仍然没有用。
String username, password, threadNum, bumpMessage, bumpTimer;
HttpClient http;
public void login() throws ClientProtocolException, IOException, NoSuchAlgorithmException
{
this.http = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://www.sythe.org/login.php?do=login");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("do", "login"));
params.add(new BasicNameValuePair("url", ""));
params.add(new BasicNameValuePair("vb_login_md5password", this.password));
params.add(new BasicNameValuePair("vb_login_md5password_utf", this.password));
params.add(new BasicNameValuePair("s", ""));
params.add(new BasicNameValuePair("vb_login_username", this.username));
params.add(new BasicNameValuePair("vb_login_password", ""));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = http.execute(httppost);
HttpEntity entity = response.getEntity();
}
public void bump() throws ClientProtocolException, IOException
{
HttpPost httppost = new HttpPost("http://www.sythe.org/newreply.php?do=postreply&t=" + this.threadNum);
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("title", ""));
params.add(new BasicNameValuePair("message", this.bumpMessage));
params.add(new BasicNameValuePair("wysiwyg", "0"));
params.add(new BasicNameValuePair("iconid", "0"));
params.add(new BasicNameValuePair("s", ""));
params.add(new BasicNameValuePair("posthash", ""));
params.add(new BasicNameValuePair("poststarttime", ""));
params.add(new BasicNameValuePair("loggedinuser", ""));
params.add(new BasicNameValuePair("multiquoteempty", ""));
params.add(new BasicNameValuePair("sbutton", "Submit Reply"));
params.add(new BasicNameValuePair("signature", "1"));
params.add(new BasicNameValuePair("parseurl", "1"));
params.add(new BasicNameValuePair("emailupdate", "9999"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = this.http.execute(httppost);
HttpEntity entity = response.getEntity();
PrintWriter writer = new PrintWriter("filename.html","UTF-8");
if (entity != null) {
InputStream instream = entity.getContent();
String line;
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(instream));
while ((line = br.readLine()) != null) {
writer.println(line);
}
} finally {
instream.close();
}
}
writer.close();
}
}
编辑:我在python中工作了!
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
def login():
logindata = urllib.urlencode({'do' : 'login',
'url':'',
'vb_login_md5password' : password,
'vb_login_md5password_utf' : password,
's' : '',
'vb_login_username': username,
'vb_login_password': '' })
response = opener.open("http://www.sythe.org/login.php?do=login", logindata)
def bumpThread():
if(username == "Not set" or password == "Not set"):
print "Please setup password!"
return
if(threadNum == 0 or message == "Not set" or bumpTime == 0):
print threadNum
print message
print "Please setup thread!"
return
logindata = urllib.urlencode({'title' : 'The Thread',
'message' : message,
'wysiwyg' : '0',
'iconid' : '0',
's' : '',
'do' : 'postreply',
't' : threadNum,
'p' : '',
'posthash' : '',
'poststarttime' : '',
'loggedinuser' : '',
'multiquoteempty' : '',
'sbutton' : 'Submit Reply',
'signature' : '1',
'parseurl' : '1',
'emailupdate' : '9999' })
thread = opener.open("http://www.sythe.org/newreply.php?do=postreply&t=" + str(threadNum),logindata)
答案 0 :(得分:0)
您可以尝试使用“可关闭的”httpclient(CloseableHttpClient
)。另外,尝试使用BasicCookieStore
实现烹饪。创建httpclient对象,执行您需要的任何请求,然后关闭 httpclient。
还有一些资源可能会有所帮助:
下面是我编写的一些示例代码,它将使用POST请求登录,然后发送一个GET请求来验证登录是否有效。
以下是我的登录功能的实现方式:
CredentialsProvider
BasicCookieStore
CloseableHttpClient
/login.html
登录。/user
执行GET请求以查看我们是否正确登录打印回复状态代码
//////// SETUP
//Setup Proxy (If needed)
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("proxy.mycompany", 8080),
new UsernamePasswordCredentials("my_username", "my_password"));
//Setup Cookies
BasicCookieStore cookieStore = new BasicCookieStore();
//Build HttpClient
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultCookieStore(cookieStore)
.build();
HttpHost target = new HttpHost("www.mycompany.com", 80, "http");
HttpHost proxy = new HttpHost("proxy.mycompany", 8080);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
try {
//////// LOGIN REQUEST, POST TO /login.html
//Start POST Request
HttpPost httppost = new HttpPost('/login.html');
//Add parameters to POST Request
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair('username','my_username'));
nameValuePairs.add(new BasicNameValuePair('password','my_password'));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Set Config for POST Request
httppost.setConfig(config);
//Execute POST Request
httpresponse = httpclient.execute(target, httppost);
//Print Response Code
try {
System.out.println("Status Code: "+httpresponse.getStatusLine().getStatusCode());
} finally {
//Close HTTP Reponse (must be in a 'finally' block)
httpresponse.close();
}
//////// CHECK LOGIN STATUS, GET TO /user
//Start GET Request
HttpGet httpget = new HttpGet('/user');
//Set Config for GET Request
httpget.setConfig(config);
//Execute GET Request
httpresponse = httpclient.execute(target, httpget);
//Print Response Code
try {
System.out.println("Status Code: "+httpresponse.getStatusLine().getStatusCode());
} finally {
//Close HTTP Reponse (must be in a 'finally' block)
httpresponse.close();
}
} finally {
httpclient.close();
}