Java HttpClient从Post方法获得响应

时间:2014-10-08 14:42:42

标签: java http-post apache-httpclient-4.x

我是HttpClient的新手,我试图在FIFA终极团队网络应用中获得玩家的价格。

使用开发人员工具,我设法找到了Post方法,从而产生了玩家的价格。

搜索特定播放器会提供以下网址请求。 https://utas.s2.fut.ea.com/ut/game/fifa15/transfermarket?maskedDefId=20801&start=0&num=16&type=player

当我查看该方法的响应时,我可以在开发者工具中看到目前市场上的所有玩家价格。

所以我尝试用Java编写代码以获得这些价格,但我的输出与Developer Tools中给出的响应完全不同。

这是我的代码:

HttpClient client = new DefaultHttpClient();
        String url = "https://utas.s2.fut.ea.com/ut/game/fifa15/transfermarket?maskedDefId=20801&start=0&num=16&type=player";
        HttpPost loginPost = new HttpPost(url);
        loginPost.setHeader("Host", "utas.s2.fut.ea.com");
        loginPost.setHeader("Connection", "keep-alive");
        loginPost.setHeader("Content-Lenght", "1");
        loginPost.setHeader("Cache-Control", "max-age=0");
        loginPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        loginPost.setHeader("Origin", "https://www.easports.com");
        loginPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36");
        loginPost.setHeader("Content-Type", "application/json");
        loginPost.setHeader("Accept", "application/json");
        loginPost.setHeader("X-UT-SID", "23c19a93-32ac-4f52-9aab-21c6be112ede");
        loginPost.setHeader("X-UT-Embed-Error", "true");
        loginPost.setHeader("X-UT-PHISHING-TOKEN", "8649502695209937021");
        loginPost.setHeader("X-HTTP-Method-Override", "GET");
        loginPost.setHeader("Referer", "https://www.easports.com/iframe/fut15/bundles/futweb/web/flash/FifaUltimateTeam.swf?cl=144469");
        loginPost.setHeader("Accept-Encoding", "gzip,deflate");
        loginPost.setHeader("Accept-Language", "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4");
        loginPost.setHeader("Cookie", "CEM-Rememberlogin=mRLLf4Ua9QJAz1J1va8yrhwlh9jFEaLsccY1Q3Ja7wREWecJnF3H348jrRpTnr3qXnhX-YKV6TH3HWgEPqgExDUP2vMFeiwkAYSkhyMuJUyHF_Fsou-fiv7Ur2XVYpqVo1KEP_dO0lLaJ5zTV1I8paWera57lPKmWxY5KzOdnRTGBbpbBv2bJOVPlGLpUFWmbOdMxElcTQga7S2-nM3icZc0JQQ2gmtDCKJ48VHSTl3x3BEuT3ltfNvFEt8elFIf; Path=/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("maskedDefId", "20801"));
        nvps.add(new BasicNameValuePair("num", "16"));
        nvps.add(new BasicNameValuePair("start", "0"));
        nvps.add(new BasicNameValuePair("type", "player"));
        loginPost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = client.execute(loginPost);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity);
        System.out.println(content);

HttpClient client = new DefaultHttpClient(); String url = "https://utas.s2.fut.ea.com/ut/game/fifa15/transfermarket?maskedDefId=20801&start=0&num=16&type=player"; HttpPost loginPost = new HttpPost(url); loginPost.setHeader("Host", "utas.s2.fut.ea.com"); loginPost.setHeader("Connection", "keep-alive"); loginPost.setHeader("Content-Lenght", "1"); loginPost.setHeader("Cache-Control", "max-age=0"); loginPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); loginPost.setHeader("Origin", "https://www.easports.com"); loginPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36"); loginPost.setHeader("Content-Type", "application/json"); loginPost.setHeader("Accept", "application/json"); loginPost.setHeader("X-UT-SID", "23c19a93-32ac-4f52-9aab-21c6be112ede"); loginPost.setHeader("X-UT-Embed-Error", "true"); loginPost.setHeader("X-UT-PHISHING-TOKEN", "8649502695209937021"); loginPost.setHeader("X-HTTP-Method-Override", "GET"); loginPost.setHeader("Referer", "https://www.easports.com/iframe/fut15/bundles/futweb/web/flash/FifaUltimateTeam.swf?cl=144469"); loginPost.setHeader("Accept-Encoding", "gzip,deflate"); loginPost.setHeader("Accept-Language", "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4"); loginPost.setHeader("Cookie", "CEM-Rememberlogin=mRLLf4Ua9QJAz1J1va8yrhwlh9jFEaLsccY1Q3Ja7wREWecJnF3H348jrRpTnr3qXnhX-YKV6TH3HWgEPqgExDUP2vMFeiwkAYSkhyMuJUyHF_Fsou-fiv7Ur2XVYpqVo1KEP_dO0lLaJ5zTV1I8paWera57lPKmWxY5KzOdnRTGBbpbBv2bJOVPlGLpUFWmbOdMxElcTQga7S2-nM3icZc0JQQ2gmtDCKJ48VHSTl3x3BEuT3ltfNvFEt8elFIf; Path=/"); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("maskedDefId", "20801")); nvps.add(new BasicNameValuePair("num", "16")); nvps.add(new BasicNameValuePair("start", "0")); nvps.add(new BasicNameValuePair("type", "player")); loginPost.setEntity(new UrlEncodedFormEntity(nvps)); HttpResponse response = client.execute(loginPost); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity); System.out.println(content);

这是开发人员工具正在展示的响应以及我需要的内容:

这是我的回答: {"auctionInfo":[{"itemData":{"id":164674417976,"timestamp":1412181858,"itemType":"player","pile":5,"resourceId":1929400641,"rating":93,"leagueId":0, etc}]}} 其中?[][][][][][][][]?V?M-.NLOU??+???Q*JM,??S?RJ?(?,JMQ([]?[]G[]Et???S??L[][]k[]??[]?6[][][]代表未知字符(方块)

所以我的问题是:如何调整代码,以便获得所请求URL的响应? 任何帮助将不胜感激

编辑: 我所要做的就是删除一些&#39; setted&#39;标题: 那些是'设置'&#39;标题我仍然需要:

[]

loginPost.setHeader("Host", "utas.s2.fut.ea.com"); loginPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36"); loginPost.setHeader("X-UT-SID", "23c19a93-32ac-4f52-9aab-21c6be112ede"); loginPost.setHeader("X-UT-Embed-Error", "true"); loginPost.setHeader("X-UT-PHISHING-TOKEN", "8649502695209937021"); loginPost.setHeader("X-HTTP-Method-Override", "GET");

但在这里,&#34; X-UT-SID&#34;和&#34; X-UT-PHISHING-TOKEN&#34;不正确,它们不是实际值(因为我从之前的会话中复制它们)。我应该如何修复我的代码以获得实际值?

提前致谢

1 个答案:

答案 0 :(得分:0)

有一些文件吗?也许您必须手动设置编码,例如:

loginPost.setHeader("Content-Type", "application/json; charset=utf-8");

也许用其他字符集改变“utf-8”(比如'iso-8859-1')