使用ETag但状态代码始终为200而不是304

时间:2013-05-28 20:28:03

标签: java facebook google-app-engine etag required

当我尝试请求时 http://graph.facebook.com/me?fields=friends 使用ETag我每次都会得到200的请求代码。 我做了两个背靠背响应并检查了它。最后它有一个分页部分,它附加了一个访问令牌。这总是不同的,所以ETag值也会发生变化。 我正在使用谷歌应用引擎来托管我的应用程序。并使用 URLFetech 发出请求。

<code> 
String ETag = FBUser.getProperty("ETag").toString();<br>
String s="https://graph.facebook.com/me?fields=friendlists&access_token=" + facebookSignedRequest.getOauth_token();<br>
URL u = null;<br>
try {<br>
u = new URL(s);<br>
} catch (MalformedURLException e) 
{
e.printStackTrace(out1);}<br>           
FetchOptions options = FetchOptions.Builder.withDefaults();<br>
HTTPRequest req = new HTTPRequest(u,HTTPMethod.GET,options);<br>
req.addHeader(new HTTPHeader("If-None-Match", ETag));<br>
URLFetchService service = URLFetchServiceFactory.getURLFetchService();<br>
HTTPResponse res = null;<br>
try 
{res = service.fetch(req);} <br>
catch (IOException e) { e.printStackTrace(out1);}<br>

String newETag="";<br>
List<HTTPHeader> Head = res.getHeaders();<br>
for(int i=0;i < Head.size();i++)<br>
{
if("ETag".equals(Head.get(i).getName()))<br>
{
   newETag=Head.get(i).getValue();<br>
   out1.println("new ETag : " + newETag);<br>
   out1.println("ETag" + ETag);<br>
}
}
if(res.getResponseCode() == 304)<br>
{ 
  return false;<br>
}else
{
FBUser.setProperty("ETag", newETag);<br>
datastore.put(FBUser);<br>
return true;<br>
}
</code>

的问候,
iDeViL17

1 个答案:

答案 0 :(得分:2)

我现在请求了。所有作品:

https://graph.facebook.com/me/friends?access_token=...

第一次请求。

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet get = HttpGet(url);
HttpResponse = httpClient.execute(get);   
Header[] headers = httpResponse.getHeaders("ETag");   
if (headers != null && headers.length() > 0)
   etags = headers[0].getValue();

第二次请求:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet get = HttpGet(url);
get.addHeader("If-None-Match", etags);
HttpResponse = httpClient.execute(get);