我正在使用Apache的HttpClient进行一次调用和两次调用autozone.com。 get调用是访问主页,以便设置JSessionId cookie。然后我试图用他们的商店定位器搜索某个邮政编码周围的商店,然后将第一个结果设置为我的“首选商店”。
我之前发现有关用户的所有信息都存储在服务器端,并使用两个永久性cookie进行查找。基本上只是一个用户名 - 密码对。这些cookie与JSessionId一起创建,因为需要JSessionId的第一篇文章完美地运行。我还发现他们也没有直接返回结果页面,而是只返回具有302“暂时移动”状态的Http标题。所以我从标题中检索新页面然后去那里。
这正是我在程序中看到的情况。成功加载对主页的获取呼叫,并设置3个必要的cookie,并检索所有发布呼叫所需的动态会话确认号。然后我对StoreLocator的处理页面进行post调用,传递所有必要的参数,获取结果的位置,进行get调用并加载该页面。我找到了“prefStoreNumber”,它是识别商店的系统唯一编号,并保存它。然后我创建了我的最后一个帖子。它将转到正确的页面以处理首选商店选择。我再次传递HTML页面上表单中的所有参数,并返回302响应。当我得到新的位置时出现问题。在HTML中,它应该是我在顶部栏中选择的商店,但它仍然与没有选择商店的情况相同。我知道cookie正确传递,否则我不会得到商店的结果。
我不确定为什么它不起作用所以任何有关可能发生的事情的想法都会受到高度赞赏。我已经包含了我的代码以防我错过了必要的东西,但我已经检查了一千次并且找不到任何错误。我使用WireShark查看帖子是否正确创建并且确实如此。
String useragent, homepage, locatorPage, host, sessionConf, results, preferedPage;
homepage = "/autozone";
locatorPage = "/autozone/storelocator/storeLocatorMain.jsp?_DARGS=/autozone/storelocator/storeLocatorSearch.jsp";
preferedPage = "/autozone/storelocator/storeLocatorMain.jsp?_DARGS=/autozone/storelocator/storeLocatorResults.jsp";
useragent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
host = "http://www.autozone.com";
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter("http.protocol.allow-circular-redirects", true);
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, useragent);
HttpContext session = new BasicHttpContext();
HttpGet get = new HttpGet(host+homepage);
get.setHeader("User-Agent", useragent);
HttpEntity e = client.execute(get, session).getEntity();
sessionConf = getHTMLValue("_dynSessConf", EntityUtils.toString(e));
EntityUtils.consume(e);
HttpPost post = new HttpPost(host+locatorPage);
List<NameValuePair> form = new ArrayList<NameValuePair>();
form.add(new BasicNameValuePair("_dyncharset", "ISO-8859-1"));
form.add(new BasicNameValuePair("_dynSessConf", sessionConf));
form.add(new BasicNameValuePair("/autozone/diy/storelocator/StoreLocatorFormHandler.successURL", "/autozone/storelocator/storeLocatorMain.jsp?targetPage=storeLocatorResults"));
form.add(new BasicNameValuePair("_D:/autozone/diy/storelocator/StoreLocatorFormHandler.successURL", " "));
form.add(new BasicNameValuePair("/autozone/diy/storelocator/StoreLocatorFormHandler.errorURL", "/autozone/storelocator/storeLocatorMain.jsp?targetPage=null"));
form.add(new BasicNameValuePair("_D:/autozone/diy/storelocator/StoreLocatorFormHandler.errorURL", " "));
form.add(new BasicNameValuePair("city", ""));
form.add(new BasicNameValuePair("_D:city", " "));
form.add(new BasicNameValuePair("state", "-1"));
form.add(new BasicNameValuePair("zip", Integer.toString(zip)));
form.add(new BasicNameValuePair("_D:zip", " "));
form.add(new BasicNameValuePair("areaCode", ""));
form.add(new BasicNameValuePair("_D:areaCode", " "));
form.add(new BasicNameValuePair("/autozone/diy/storelocator/StoreLocatorFormHandler.findNearestLocations", "submit"));
form.add(new BasicNameValuePair("_D:/autozone/diy/storelocator/StoreLocatorFormHandler.findNearestLocations", " "));
form.add(new BasicNameValuePair("_DARGS", "/autozone/storelocator/storeLocatorSearch.jsp"));
post.setEntity(new UrlEncodedFormEntity(form, "UTF-8"));
HttpResponse response = client.execute(post, session);
results = response.getFirstHeader("Location").toString().substring(10);
EntityUtils.consume(response.getEntity());
get = new HttpGet(results);
response = client.execute(get, session);
e = response.getEntity();
String storeID = getHTMLValue("prefStoreNumber", EntityUtils.toString(e));
EntityUtils.consume(e);
post = new HttpPost(host+preferedPage);
form = new ArrayList<NameValuePair>();
form.add(new BasicNameValuePair("_dyncharset", "ISO-8859-1"));
form.add(new BasicNameValuePair("_dynSessConf", sessionConf));
form.add(new BasicNameValuePair("/autozone/diy/storelocator/StoreLocatorFormHandler.preferedStoreSuccessURL", "/autozone/storelocator/storeLocatorMain.jsp?targetPage=storeLocatorResults"));
form.add(new BasicNameValuePair("_D:/autozone/diy/storelocator/StoreLocatorFormHandler.preferedStoreSuccessURL", " "));
form.add(new BasicNameValuePair("/autozone/diy/storelocator/StoreLocatorFormHandler.errorURL", "/autozone/storelocator/storeLocatorMain.jsp?targetPage=storeLocatorResults"));
form.add(new BasicNameValuePair("_D:/autozone/diy/storelocator/StoreLocatorFormHandler.errorURL", " "));
form.add(new BasicNameValuePair("prefStoreNumber", storeID));
form.add(new BasicNameValuePair("/autozone/diy/storelocator/StoreLocatorFormHandler.setPreferedStore", "submit"));
form.add(new BasicNameValuePair("_D:/autozone/diy/storelocator/StoreLocatorFormHandler.setPreferedStore", " "));
form.add(new BasicNameValuePair("_DARGS", "/autozone/storelocator/storeLocatorResults.jsp"));
post.setEntity(new UrlEncodedFormEntity(form, "UTF-8"));
response = client.execute(post, session);
displayResponse(response);
results = response.getFirstHeader("Location").toString().substring(10);
EntityUtils.consume(response.getEntity());
get = new HttpGet(results);
response = client.execute(get, session);
displayResponse(response);
displayResponse仅用于调试。它列出了所有响应标题,后跟行分隔符和主HTML。对不起啰嗦,但我想确保我的问题得到很好的理解。
更新:好的。我在浏览器请求和我的程序上运行了WireShark。我发现我错过了一个简单的错误。当我从HTML中检索prefStoreNumber时,我意外地抓住了太多并且随机存储了随机HTML。我将解决这个问题,看看它是否有效,但也存在其他一些差异。我在下面列出了它们。他们中的任何一个会给我带来麻烦吗? 字段:浏览器|程序
Cache-Control: max-age=0|(N/A)
Origin: http://www.autozone.com|(N/A)
Content-Type: application/x-www-form-urlencoded|(N/A)
Accept: text/html,application/xhtml+xml,application/x ml;q=0.9,*/*;q=0.8|(N/A)
Referer: http://www.autozone .com/autozone/storelocator/storeLocatorMain.jsp? targetPage=storeLocatorResults&_ requestid=3010533|(N/A)
Accept-Encoding: gzip,deflate,sdch|(N/A)
Accept-Language: en-US,en ;q=0.8|(N/A)
Accept-Charset: ISO-8859 -1,utf-8;q=0.7,*;q=0.3|(N/A)
Cookie2: (N/A)|$Version=1
更新2:正确解析prefStoreNumber后,它工作得很好。