我有一個http請求的問題,我想你可以幫助我。 我有一個JavaEE Web應用程序,我需要提出一些請求。由於java中的會話,兩個HTTP請求(一個接一個)不工作
特別是,我有兩個請求做一個接一個。但爲了取得它,webapp(鏈接到我公司的另一個webapp)希望請求來自「兩個不同的來源」。例如,如果我使用相同的瀏覽器執行這些請求,它將不起作用,而如果我使用mozilla做第一個,然後使用mozilla在「隱身窗口」中執行第二個,它將起作用!奇怪是不是?
所以我想用與java的發佈請求相同的策略(我正在開發一個谷歌玻璃的應用程序),我無法設法做到這一點。我嘗試了很多技巧。這是我最後的代碼,我用掛本身鏈接到的CookieStore情境的HttpClient的,我清除cookie存儲=>儘管如此,它不工作...
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;
}
在活動:
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
僅供參考,錯誤並非來自Java,但由我公司的web應用,因爲它檢測到類似的會議(我不知道這個詞「會話」的...)
感謝您幫幫我 !
UPDATE
Maven依賴:
<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>
你的進口產品是什麼樣的? –
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
他們來自maven依賴關係:特別是:httpcore-4.3.2,httpclient-4.3.3,httpmime-4.3。3 – supermarco