开发者

How should I troubleshoot my faulty HTTP log in?

I'm trying to use the Apache HttpComponents Client to simulate, in Java, a login process on a website. It's failing at the moment and I'm not sure why. I'm using Firebug in Firefox to try and understand the equivalent browser events and I'm sure it's logging in by a particular POST method on a form.

Here's my code. At the end I expect a whole bunch of cookies to be set, but they are not, and the responses I'm getting to subsequent GET requests prove 开发者_开发知识库that the login failed.

String email = ...
String password = ...
String url = ...
DefaultHttpClient client = new DefaultHttpClient();
List<NameValuePair> loginParams = new ArrayList<NameValuePair>(2);
loginParams.add(new BasicNameValuePair("logindetails[email]", email));
loginParams.add(new BasicNameValuePair("logindetails[password]", password));
UrlEncodedFormEntity loginEntity = new UrlEncodedFormEntity(loginParams);
HttpPost loginPost = new HttpPost(url);
loginPost.setEntity(loginEntity);
HttpResponse loginResponse = client.execute(loginPost);
HttpEntity loginRepsonseEntity = loginResponse.getEntity();
loginRepsonseEntity.consumeContent();

for (Cookie cookie: client.getCookieStore().getCookies()) {
    System.out.println(cookie.getName() + " -> " + cookie.getValue());
}

Where should I start looking for the problem? I'd like to verify that the POST is the same as the one my browser is making -- what's the best way to do that?


We use the Apache HttpClient stuff every day where I work, it's wonderful. What you're doing, where you are encoding the data yourself, looks a little weird to me. There is an easier way. I'll assume you're doing a POST.

String email = ...
String password = ...
String url = ...

PostMethod post = new PostMethod(url);

NameValuePair[] data = {
    new NameValuePair("logindetails[email]", email),
    new NameValuePair("logindetails[password]", password)
};

post.setRequestBody(data);

// execute method and handle any error responses.

HttpClient httpclient = new HttpClient();

int statusCode = httpclient.executeMethod(post);

// continue as neccessary

I took that code (and modified it to fit yours better) from the Apache HttpClient site. Unless you have a good reason, you shouldn't need to bother packaging up the fields yourself with the UrlEncodedFormEntity.

As for debugging this kind of stuff, it can be easiest to watch using WireShark to watch the packets go back and forth. Any of these will let you watch the requests go back and forth (as long as you don't use HTTPS) so you can easily tell what's being sent incorrectly by comparing it to what a browser sends. It's not the user friendliest program in the world, but it's invaluable when trying to figure out this kind of stuff.


You can use Firebug in Firefox to look at the Net-tab to investigate how the post is done when you use a web page. Looking at your code, my guess is that your parameter names are strange. logindetails[email] and logindetails[password] look weird.

This would be the equivalent of having:

<form action="...url" method="post">
   email: <input type="text" name="logindetails[email]"/><br/>
   password: <input type="password" name="logindetails[password]"/><br/>
   <input type="submit" value="submit"/>
</form>

Make sure that's what you intended...


Look at the wonderful cURL utility -> http://curl.haxx.se/docs/manpage.html.

It's ubiquitous in Unix/Linux and is available under Cygwin on Windows.

It lets you achieve what you are trying to do in Java in a few keystrokes from command line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜