我遇到了http请求的问题,我想你可以帮助我。 我有一个JavaEE webapp,我需要在其上提出一些请求。
特别是,我有两个要求一个接一个地做。但是为了取消它,webapp(它链接到我公司的另一个webapp)希望请求来自两个不同的起源"。例如,如果我使用相同的浏览器执行这些请求,它将无法工作,而如果我使用mozilla执行第一个请求,然后使用mozilla执行第二个请求"隐身窗口",它将起作用!奇怪的不是吗?
所以我想使用与java发布请求相同的策略(我正在开发谷歌眼镜的应用程序),我无法做到这一点。我尝试了很多技巧。这是我的最后一个代码,我使用链接到链接到cookiestore的上下文的httpclient,我清除了cookie store =>然而,它不起作用......
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,而是来自我公司的webapp,因为它会检测到类似会话的内容(我不确定单词" session" ...)
感谢您的帮助!
更新
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)
您可以尝试通过为两个请求设置不同的用户代理来模仿所需的行为(来自2个不同浏览器的2个请求),例如:
httpPost.setHeader("User-Agent", "MyAgent");
答案 1 :(得分: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;
}
}
请注意,我没有在任何地方指定字符编码,因此您可能希望添加这些字符编码以及一些异常处理,以使事情更加健壮。