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>