1

I have a problem with a http request and I think you can help me. I have a JavaEE webapp on which I need to make some requests.

Particularly, I have two requests to do one after the other. But in order to suceed it, the webapp (which is link to another webapp from my company) wants the requests to come from "two different origins". For example, if I do those requests with the same browser, it won't work whereas if I do the first one with mozilla then the second one with mozilla in "incognito window", it will work ! Weird isn't it ?

So I want to use the same strategy with a post request with java (I'm developing an app for google glass) and I cannot manage to do it. I try a lot of tricks. Here is my last code, I use a httpclient linked to a context itself linked to a cookiestore and I cleared the cookie store => nevertheless it doesn't work...

  public RestClient(){
    httpClient = new DefaultHttpClient();
    localContext = new BasicHttpContext();
    cookieStore = new BasicCookieStore();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    }

  private String postHttp(String address, String content, String contentType) {
    String text = null;
    HttpResponse response;
    try {
      HttpPost httpPost = new HttpPost(address);
      httpPost.setHeader("Content-Type", contentType);
      httpPost.setEntity(new StringEntity(content));
      response = httpClient.execute(httpPost, localContext);
      text = HttpUtil.convertInputStreamToString(response);
      cookieStore.clear();
    }
    catch (Exception e) {
      return e.getLocalizedMessage();
    }
    return text;
  }

In the activity :

postData = "myrequest1";
RestClient a = new RestClient();
String result1 = a.postHttp(Constants.AVAIL_ADDRESS, postData, "application/x-www-form-urlencoded;charset=UTF-8");
a = null;
//fine

postData = "myrequest2";
RestClient b = new RestClient();
String result2 = b.postHttp(Constants.BOOKING_ADDRESS, postData, "application/x-www-form-urlencoded;charset=UTF-8");
//error

FYI, the error doesn't come from java but from my company webapp because it detects something like the sessions (I'm not sure of the word "session"...)

Thank you for your help !

UPDATE

Maven dependencies :

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.3.3</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.3.3</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpcore</artifactId>
  <version>4.3.2</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.2.3</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.2.3</version>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.8.2</version>
  <scope>test</scope>
</dependency>
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
supermarco
  • 81
  • 6
  • What do your imports look like? – Robby Cornelissen Jul 03 '14 at 09:06
  • import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.ClientContext; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.ByteArrayBody; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; – supermarco Jul 03 '14 at 09:07
  • they are from maven dependencies : particularly : httpcore-4.3.2, httpclient-4.3.3, httpmime-4.3.3 – supermarco Jul 03 '14 at 09:08
  • Both `DefaultHttpClient` and `ClientContext` are deprecated in those versions. – Robby Cornelissen Jul 03 '14 at 09:29
  • ok but what class do I have to use ? I cannot find stg clear and functional – supermarco Jul 03 '14 at 09:31

2 Answers2

0

You can try to mimic the required behavior (2 request from 2 different browsers) by setting different user agent for both requests Like:

httpPost.setHeader("User-Agent", "MyAgent");
Umer Hayat
  • 1,993
  • 5
  • 31
  • 58
0

Not sure where the exact problem lies, but I think it's related to the way the cookie store is being set in the context. I've also noticed that a lot of the classes you're using are deprecated.

This should get you on your way though:

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder;

public class RestClientToo {
    private HttpClient client = HttpClientBuilder.create()
            .disableCookieManagement().build();
    private BasicResponseHandler responseHandler = new BasicResponseHandler();

    public String postHttp(String address, String content, String contentType)
            throws ClientProtocolException, IOException {

        HttpPost post = new HttpPost(address);
        post.setHeader("Content-Type", contentType);
        post.setEntity(new StringEntity(content));

        HttpResponse response = client.execute(post);
        String result = responseHandler.handleResponse(response);

        return result;
    }
}

Note that I haven't specified character encodings anywhere, so you might want to add those, as well as some exception handling, to make things a bit more robust.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • thank you but unfortunately it doesn't work. No, the request doesn't begin, I have directly this error : Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE – supermarco Jul 03 '14 at 10:10
  • You have conflicting JAR files on your classpath. See this question: http://stackoverflow.com/questions/21864521/java-lang-nosuchfielderror-org-apache-http-message-basiclineformatter-instance – Robby Cornelissen Jul 03 '14 at 10:12
  • thank you, I just "cleaned" my dependencies and I still have the same error, by the way, when I use ClassLoader classLoader = RestClient.class.getClassLoader(); URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class"); System.out.println(resource);, it returns "null"... I put an update with my dependencies – supermarco Jul 03 '14 at 10:35
  • Just tried with the same dependencies as you and ran without a hitch. – Robby Cornelissen Jul 03 '14 at 10:46
  • ok i extract my restclient class in order to put it directly in my android project (no more maven dependencies now) and it still doesn't work... – supermarco Jul 03 '14 at 10:58