在过去的几个小时里,我一直在处理一个问题,但我似乎无法在我的一生中解决这个问题。
我有一个应用程序将用户登录到我的服务器,并将一些数据添加到 httppost 实体中。第一次将数据添加到实体中时一切正常。第二次将任何 post 请求发送到服务器时,客户端会挂起直到超时。
我的 DefaultHttpClient 作为全局变量存储在我的应用程序中,并在调用 load_client() 时加载到我的类中。当pairs 变量中没有任何内容时一切正常,但是一旦第二次在setEntity 中设置pairs 变量,程序就永远不会到达“收到响应”消息。
httppost = new HttpPost(format_url(base_url,controller));
logger.info("Sending Post Request");
load_client();
try {
logger.info("Setting Entity");
httppost.setEntity(new UrlEncodedFormEntity(pairs,"UTF-8"));
for(NameValuePair pair : pairs){
logger.info(pair.getName() + " : " + pair.getValue());
}
logger.info("URL Encoded");
response = httpclient.get_client().execute(httppost);
logger.info("Received response");
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
logger.info("Status: " + statusLine.toString());
is = entity.getContent();
logger.info("Content: " + is.toString());
}catch(Exception e){
logger.error("Error in http connection "+e.toString());
}
任何帮助,将不胜感激。除了保存旧客户端的 cookie 并在每次我想发出 Post 请求时将新对象传递给应用程序之外,我现在真的不知道该怎么做。这似乎是解决问题的不好方法。
编辑:这是客户端超时后的消息。当 post 请求中没有数据时,它永远不会被抛出。
Error in http connection org.apache.http.NoHttpResponseException: The target server failed to respond
编辑:好吧,我把这个错误缩小到一行。
DefaultHttpClient defhttpClient = new DefaultHttpClient();
logger.info("URL Encoded");
//When I pass the cookies things stop working...
//defhttpClient.setCookieStore(httpclient.get_cookies());
response = defhttpClient.execute(httppost);
logger.info("Received response");
如果我创建一个新的 DefaultHttpClient 并使用参数运行 post 请求,则客户端可以与服务器正常通信。唯一的问题是我需要来自旧客户端的 cookie 来告诉服务器当前用户是谁。当我从旧客户端获取 cookie 并将它们提供给新客户端时,应用程序挂起。我什至没有看到请求进入我的服务器。我想我可能只需要为这个发布一个新帖子......