3

当我尝试使用 ETag 请求 http://graph.facebook.com/me?fields=friends 时,我每次都会收到请求代码为 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

4

1 回答 1

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);   
于 2013-06-07T23:59:03.190 回答