强制执行新的HTTP响应

时间:2018-10-18 12:10:50

标签: java httprequest httpresponse

我有一种发送请求并从网站获取响应的方法。

响应每4小时更改一次。

我在两台计算机上具有相同的Java代码。

  • 在第一个响应上,即使在现在,响应仍然与24小时前相同。
  • 编辑:第二个也有一个较旧的(缓存的)响应,但是一天后(当我第一次启动Programm时)。

如果我尝试通过此网站(https://apitester.com/)执行相同的请求/响应,则每次都能获得预期的结果。但是我的程序没有。

我到处都使用相同的设置,例如网站URL和用户代理。

那我怎样才能强制使用新的,新的响应版本? (Cloudflare可能会缓存一次使用过的IP地址的网站吗?)

编辑版本(仍然无法使用):

 String url = "WEBSITE_PLACEHOLDER";
        final String USER_AGENT = "AGENT_PLACEHOLDER";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // add reuqest header
       con.setRequestProperty("Cache-Control", "no-cache");
       con.setRequestProperty("Cache-Control", "must-revalidate");
       con.setUseCaches(false);    
       con.setRequestMethod("POST");
       con.setRequestProperty("User-Agent", USER_AGENT);
       con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        // Send post request

        int responseCode = con.getResponseCode();

            if(responseCode == 200) {    
            BufferedReader in = new BufferedReader(new 
            InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //System.out.println(response.toString());
            return response.toString();

            }
            else {
                throw new Exception();
            }
        }

@麦克  -两个系统上运行的标题和正文响应

第一台计算机(现在甚至只获得旧的响应正文

我将用“ 这就是我得到的这就是我需要的”标记所需的令牌

Printing Response Header...

Transfer-Encoding ,Value : [chunked]

null ,Value : [HTTP/1.1 200 OK]

Strict-Transport-Security ,Value : [max-age=5184000]

CF-RAY ,Value : [46bb5ec486df978c-FRA]

Server ,Value : [cloudflare]

Connection ,Value : [keep-alive]

Vary ,Value : [Accept-Encoding]

Set-Cookie ,Value : [__cfduid=d77d33fbc1ca290cd37b93c3eacff74c31539869538; 
expires=Fri, 18-Oct-19 13:32:18 GMT; path=/; domain=."website"; HttpOnly]

Date ,Value : [Thu, 18 Oct 2018 13:32:18 GMT]

Content-Type ,Value : [application/json; charset=utf-8]
Server - cloudflare


Get Response Body: 

{"response":{"token":"26557695w66YornObzClheLDDBTd","ruleset":"guest","nextPing":562312,


//This is what i get 
    "signed":"**eyJkYXRhIjoie1widGltZVwiOjE1Mzk4Njk1MzgwNTAsXCJ2YWxpZFVudGlsXCI6MTUzOTg3MDEzODA1MCxcImlwc1wiOltcIjkwLjE4Ny4zNS45M1wiXSxcInJ1bGVzZXRcIjpcImd1ZXN0XCIsXCJ2ZXJpZmllZFwiOmZhbHNlLFwiZXJyb3JcIjpudWxsLFwiYXBwXCI6e1wicGxhdGZvcm1cIjpudWxsLFwidmVyc2lvblwiOm51bGwsXCJzZXJpdmNlXCI6bnVsbH19Iiwic2lnbmF0dXJlIjoicEhsWllHcHNOQnZFVTZ1N1BWQk5uZmo4RDlpZlV5L2RneWx3TDcrdjE3NDNMQndPSGpRZEFWRkdLNmNqSCt2amtBTVd6eWZtZUh4TXN1ZWttdXJEQ3NPNXVtN0FjM0RKSFNneDFzRmZNN2ZORFJRSkY5TFpHSmJBNnEvSWtyQ0NEM01sdlpjTlNic3lhT3FGbDlZNWVaZXpHTHd1REZoUjVkZ0QyRFFaK3RRPSJ9**"}}

第二台计算机(相同的程序,新的响应)

Printing Response Header...

Transfer-Encoding ,Value : [chunked]

null ,Value : [HTTP/1.1 200 OK]

Strict-Transport-Security ,Value : [max-age=5184000]

CF-RAY ,Value : [46bb5a3f1427234e-FRA]

Server ,Value : [cloudflare]

Connection ,Value : [keep-alive]

Vary ,Value : [Accept-Encoding]

Set-Cookie ,Value : [__cfduid=d0ba210989762b597b0149f82d524ee371539869352; expires=Fri, 18-Oct-19 13:29:12 GMT; path=/; domain="website"; HttpOnly]

Date ,Value : [Thu, 18 Oct 2018 13:29:12 GMT]

Content-Type ,Value : [application/json; charset=utf-8]

Server - cloudflare


Get Response Body: 

{"response":{"token":"265576292ukGhcUmhtl8rDBrDJtV","ruleset":"guest","nextPing":834688,


//This is what i need  

“签署”: “eyJkYXRhIjoie1widGltZVwiOjE1Mzk4NjkzNTI4ODAsXCJ2YWxpZFVudGlsXCI6MTUzOTg2OTk1Mjg4MCxcImlwc1wiOltcIjg3LjEyMy4xMi4yNDNcIl0sXCJydWxlc2V0XCI6XCJndWVzdFwiLFwidmVyaWZpZWRcIjpmYWxzZSxcImVycm9yXCI6bnVsbCxcImFwcFwiOntcInBsYXRmb3JtXCI6bnVsbCxcInZlcnNpb25cIjpudWxsLFwic2VyaXZjZVwiOm51bGx9fSIsInNpZ25hdHVyZSI6IkllRSsrN01GblNheE9VWjZDdWtsU2lUb1RCcFI3VTZQZWJyNlE5UU1jR3ZzSExoeDdZWGFmSytreGJqVzNMSnVCb0hMM3hGMWpHQjVuVWtiRzh2Yys3dkMrR3hBckRETnkvMmlpbXhaa3I4Yk1OZE5QQThIamxFR0FON2V5UlQ4VWNGR05ES1QwaUVtQ1dJVXZiZjhJbW5xaStDR0QwUC9LR3RDYnFIVGFVYz0ifQ ==”}}

2 个答案:

答案 0 :(得分:0)

响应来自CloudFlare,这很可能会缓存来自原始服务器的响应。您需要确保您的请求指示服务器以及沿途的所有代理服务器不要使用缓存。您应该可以使用Cache-Control标头来完成此操作。

https://stackoverflow.com/a/14544664/657224

答案 1 :(得分:0)

解决方案是这两个命令

con.setDefaultUseCaches(false);
con.setUseCaches(false);