2014-07-03 29 views
1

我有一個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> 
+0

你的進口產品是什麼樣的? –

+0

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

+0

他們來自maven依賴關係:特別是:httpcore-4.3.2,httpclient-4.3.3,httpmime-4.3。3 – supermarco

回答

0

您可以嘗試通過設置不同的用戶代理的兩個請求像模仿所需的行爲(從2級不同的瀏覽器2請求):

httpPost.setHeader("User-Agent", "MyAgent"); 
+0

如果會話cookie正在發送,這不會有所幫助。 –

0

不知道確切的問題出在哪裏,但我認爲它與Cookie存儲在上下文中的設置方式有關。我也注意到很多你使用的類都被棄用了。

這應該讓你的方式,雖然:

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; 
    } 
} 

請注意,我沒有指定字符編碼的任何地方,所以你可能要添加這些,以及一些異常處理,以使事情有點強勁。

+0

謝謝你,但不幸的是它不起作用。不,請求沒有開始,我直接有這個錯誤:引起:java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE – supermarco

+0

您的類路徑中存在衝突的JAR文件。看到這個問題:http://stackoverflow.com/questions/21864521/java-lang-nosuchfielderror-org-apache-http-message-basiclineformatter-instance –

+0

謝謝,我只是「清理」我的依賴關係,我仍然有相同的錯誤,順便說一下,當我使用ClassLoader classLoader = RestClient.class.getClassLoader(); URL resource = classLoader.getResource(「org/apache/http/message/BasicLineFormatter.class」); System.out.println(resource);,它返回「null」... 我對我的依賴關係進行了更新 – supermarco

相關問題