使用ETag,但状态代码始终为200而不是304 [英] using ETag but the status code is always 200 and not 304

查看:121
本文介绍了使用ETag,但状态代码始终为200而不是304的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试要求
http://graph.facebook。 com / me?fields = friends
与ETag我总是得到请求代码为200每次。
我做了两个背靠背的响应并检查。这个变化到底是它有一个分页的部分,它附有一个访问令牌。这是永远不同的,所以ETag值也改变。
我正在使用谷歌应用引擎托管我的应用程序。并使用 URLFetech 进行请求。

When I try to request for the the http://graph.facebook.com/me?fields=friends with the ETag I always get the request code as 200 every time. we I made two back to back response and checked it. The change was at the end it had a section for pagination and it attached a access token. which is always different so the ETag value also changes. I am using google app engine to host my app. and using URLFetech to make the request.

<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>

regards,

iDeViL17

regards,
iDeViL17

推荐答案

我现在请求尝试。所有作品:

I try now with request. All works:

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);   

这篇关于使用ETag,但状态代码始终为200而不是304的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆