Geek
Geek

Reputation: 3319

HttpClient statusCodes

Im using HttpCLient to autoLogin a website. I an getting statusCode as '200'. API says SC 200 - OK. what does that mean? Login is established? When I see the list of statusCodes there is SC Accepted - 202. What is the difference between Accepted and OK. If login is established what status code should I get? Please help.

pesudo code if this helps to answer:

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class HttpTest {
    public static void main(String args[]) throws HttpException, IOException {

    HttpClient client = new HttpClient();

    // make the initial get to get the SESSION cookie
    GetMethod get = new GetMethod(
    "http://www.yahoo.com/");
    client.executeMethod(get);
    get.releaseConnection();

    // authorize
    PostMethod post = new PostMethod(
    "https://login.yahoo.com/config/login?");
    NameValuePair[] data = {
    new NameValuePair("login", "[email protected]"),
    new NameValuePair("passwd", "bbb")
    };
    post.setRequestBody(data);
    client.executeMethod(post);
    post.releaseConnection();

    //resubmit the original request
    client.executeMethod(get);
    String response = get.getResponseBodyAsString();
    get.releaseConnection();
    System.out.println("Status Code :::"+get.getStatusCode());
    System.out.println(response);    
    }
}

This is the form based authentication i hvae been trying. I am getting the same issue here...200-ok for improper credentials. Im using a common website like yahoo to login. Any advice?

Upvotes: 2

Views: 8290

Answers (5)

opossum
opossum

Reputation: 1

We were using following code for status code outputs. Note that classes first is a pojo objects from server response:

public void doFirst(int i) throws ClientProtocolException, IOException {

HttpClient client = HttpClientBuilder.create().build();

HttpGet request = new HttpGet("url" + i);
request.addHeader("content-type", "application/json");
HttpResponse response1 = client.execute(request);
if(response1.getStatusLine().getStatusCode() != 200){
    System.err.println(response1.getStatusLine().getStatusCode());
}
String json = EntityUtils.toString(response1.getEntity(), "UTF-8");
Gson gson = new Gson();
first ress = gson.fromJson(json, first.class);
System.out.println("Incoming data: " + ress.getNumber());

/////some action///////
HttpPost post = new HttpPost("url" + i);
firstSol sol= new firstSol();
sol.setNumber(k);
System.out.println("Data to send : " + gson.toJson(sol) );
post.setEntity(new StringEntity(gson.toJson(sol)));
client.execute(post);
HttpResponse response2 = client.execute(request);
if(response2.getStatusLine().getStatusCode() != 200){
System.err.println(response2.getStatusLine().getStatusCode());
}
}

you might have a look at following maven dependencies for this to work:

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>
<dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>

Upvotes: 0

David Perry
David Perry

Reputation: 1364

Java's HttpClient status codes are the same as the status codes defined in RFC1945 (HTTP 1.0), RFC2616 (HTTP 2.0) and RFC2618 (WebDAV).

These specific codes mean:

200 OK

The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:

GET an entity corresponding to the requested resource is sent in the response;

HEAD the entity-header fields corresponding to the requested resource are sent in the response without any message-body;

POST an entity describing or containing the result of the action;

TRACE an entity containing the request message as received by the end server.

202 Accepted

The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility for re-sending a status code from an asynchronous operation such as this.

The 202 response is intentionally non-committal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed. The entity returned with this response SHOULD include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.

For definitions of the other common HTTP status codes, see RFC2616

Upvotes: 4

Freiheit
Freiheit

Reputation: 8757

Review this list: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

But as milan stated you should review the API and make sure the API is using those error codes the same way the standard says to use them.

For example, you could try a login get a 200, but the payload returned has a login failure message.

A better behaved implementation would return a 200 for a good login and a 401 or 403 on a bad login.

Upvotes: 0

David
David

Reputation: 218798

200 means the request was successful and the response is based on that successful request. 202 would mean that the request was successful and has been queued for processing on the server, so the response isn't based on successful completion of the request because it hasn't necessarily completed yet.

Think of it like a short conversational exchange...

200:

Client: Here is my request
Server: Thanks! I've processed your request. Here's the response you were looking for.

vs.

202:

Client: Here is my request
Server: Thanks! I'm not done processing it yet, but I'm just letting you know that I've received it and it's under way.

I can't imagine a login taking a long time, so I'd expect a successful login to always return a 200 response.

Upvotes: 1

kosa
kosa

Reputation: 66637

202 Accepted means, your request is getting processed by the receiver (something like I got your application will look into it).

200 OK means, Your application was processed and granted what you requested (whether it could be login (or) request for another resource).

Upvotes: 0

Related Questions