使用“If-None-Match”标题创建HttpWebRequest(Etag) [英] Making HttpWebRequest with “If-None-Match” Header (Etag)

查看:67
本文介绍了使用“If-None-Match”标题创建HttpWebRequest(Etag)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用normall从网上下载数据httpwebrequest(适用于大多数网址),但服务器发送意外回复,如此

 HTTP / 1.1 200 OK 
服务器:nginx
日期:星期四,2017年8月10日03:59:39 GMT
内容类型:application / json
内容长度:1984
连接:keep-alive
访问-Control-Allow-Headers:Content-Type,Authorization,X-Requested-With,X-CSRFToken,If-None-Match-,X-API-SOURCE
Vary:Accept-Encoding,Cookie
ETag:"6f15f338d170c7f833c491beb546e4a9; gzip"
Access-Control-Allow-Credentials:true
Access-Control-Allow-Methods:POST,GET,OPTIONS,PUT,DELETE
Set-Cookie:SPC_SI = ycl079eb0umjw270xoybjhok3hz6hp7x; expires =星期五,2017年8月11日03:59:39 GMT;仅Http;最大年龄= 86400; Path = /


 {
" itemid":62790979,
" likes":false,
" ; offer_count" ;: 0,
"影片] ;: [],
" rating_star" ;: 5.187136305269734,
"图像" ;:" 6ebb860db6 ????????? ????????????,,
" image_list":[" 6ebb860db6 ????????????????????? ;],
" is_adult":null,
" can_use_wholesale":false,
" currency":" IDR",
" rcount_with_context":6,
" show_free_shipping":1,
" estimated_days":3,
" show_discount":0,
" price_before_discount":0,
" cmt_count" ;: 16,
" is_shopee_verified" ;:假,
" CATID" ;: 202,
" is_official_shop" ;:假,
" ; price_min":null,
" rating_count":[10,0,0,2,3,5],
" likes_count":13,
" version":" ; 6f15f338d170c7f833c491beb546e4a9",
" coin_earn_items":[],
" price_min_before_discount":0,
" cb_option":0,
" price":null,
" hashtag_list":[
" #editin ????????????????,,
" #soft ????????? ??",
" #sof ??????????,,
" #pal ?????????,
" #pal ?????????,,
" #co ??????",
" #co ???????,",
" #prog ???????????,,
" #juals ?????????????,,
" #ins ????????",
" #fl ??????",
"#??",
" #pal ?? ??????","
" #U ????"
],
" price_max":null,
" description":" Corel Draw Video X9 32& 64位\ nn2。友立会声会影v11 Plus \\\
3。友立会声会影v11 Plus \\\
4。索尼Ve ????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????" ;,
" flash_sale" ;:空,
"模型" ;: [],
"品牌" ;:"" ;,
" rcount_with_image" ;: 1,
" shopid":3732962,
" discount":null,
" has_lowest_price_guarantee":false,
" sold":4,
" price_max_before_discount":0,
" sub_catid":443,
" condition":1,
" name":" Packet ???????????? ????????????,"
"批发_tier_list" ;: [],
" ITEM_STATUS" ;: [否????" ;,
" third_catid" ;: 6910,
" is_pre_order" ;:假,
" coin_spend_cash_unit":null,
" attributes":[],
" stock":118
}


正如你所看到的,有些字段与恼人的" 结合在一起? "
我使用Fidler检查标准浏览器请求,与我的请求唯一不同的是浏览器发送"
If-None-Match - " header
到服务器。问题是如何填充该标题,因为我尝试从之前的请求发送etag标题但我仍然得到响应"
"
characters。




这是我的请求代码


<预类=" prettyprint"> HttpWebRequest的网络;
HttpWebResponse RESP;
的StreamReader阅读器;
Web =(HttpWebRequest)WebRequest.Create("" https://shopee.co.id/api/v1/item_detail/?item_id = 262740376& shop_id = 13072340");
Web.CookieContainer = New CookieContainer();
Web.Method =" GET";
Web.UserAgent =" Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,与Gecko一样)铬/ 42.0.2311.135 Safari浏览器/ 537.36边沿/ 12.10240英寸;
Web.KeepAlive = TRUE;
Web.Accept = QUOT;应用/ JSON英寸;
Web.ContentType = QUOT;应用/ json; Charset = UTF-8" ;;
Web.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
Web.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
Web.Headers。添加("x-requested-with","XMLHttpRequest") ;
Web.Headers.Add(" x-api-source"," pc");

Resp =(HttpWebResponse)Web.GetResponse();
Reader = new StreamReader(Resp.GetResponseStream());
string Res = Reader.ReadToEnd();


此代码适用于第一个5/6请求。之后,服务器将发送恼人的" "
如上所述。


希望你能理解我糟糕的语言。

解决方案

有人请吗?


I am trying do download data from web with normall httpwebrequest (work on most url), but server send me unexpected response, like this

HTTP/1.1 200 OK
Server: nginx
Date: Thu, 10 Aug 2017 03:59:39 GMT
Content-Type: application/json
Content-Length: 1984
Connection: keep-alive
Access-Control-Allow-Headers: Content-Type,Authorization,X-Requested-With,X-CSRFToken,If-None-Match-,X-API-SOURCE
Vary: Accept-Encoding, Cookie
ETag: "6f15f338d170c7f833c491beb546e4a9;gzip"
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE
Set-Cookie: SPC_SI=ycl079eb0umjw270xoybjhok3hz6hp7x; expires=Fri, 11-Aug-2017 03:59:39 GMT; httponly; Max-Age=86400; Path=/

{
        "itemid": 62790979,
        "liked": false,
        "offer_count": 0,
        "videos": [],
        "rating_star": 5.187136305269734,
        "image": "6ebb860db6?????????????????????",
        "image_list": ["6ebb860db6?????????????????????"],
        "is_adult": null,
        "can_use_wholesale": false,
        "currency": "IDR",
        "rcount_with_context": 6,
        "show_free_shipping": 1,
        "estimated_days": 3,
        "show_discount": 0,
        "price_before_discount": 0,
        "cmt_count": 16,
        "is_shopee_verified": false,
        "catid": 202,
        "is_official_shop": false,
        "price_min": null,
        "rating_count": [10, 0, 0, 2, 3, 5],
        "liked_count": 13,
        "version": "6f15f338d170c7f833c491beb546e4a9",
        "coin_earn_items": [],
        "price_min_before_discount": 0,
        "cb_option": 0,
        "price": null,
        "hashtag_list": [
            "#editin??????????????",
            "#soft??????????",
            "#sof?????????",
            "#pal????????",
            "#pal????????",
            "#co??????",
            "#co???????",
            "#prog??????????",
            "#juals????????????",
            "#ins????????",
            "#fl??????",
            "#??",
            "#pal????????",
            "#u????"
        ],
        "price_max": null,
        "description": "Corel Draw Video X9 32 & 64 Bit\n2. Ulead VideoStudio v11 Plus\n3. Ulead VideoStudio v11 Plus\n4. Sony Ve?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????",
        "flash_sale": null,
        "models": [],
        "brand": "",
        "rcount_with_image": 1,
        "shopid": 3732962,
        "discount": null,
        "has_lowest_price_guarantee": false,
        "sold": 4,
        "price_max_before_discount": 0,
        "sub_catid": 443,
        "condition": 1,
        "name": "Packet????????????????????????",
        "wholesale_tier_list": [],
        "item_status": "no????",
        "third_catid": 6910,
        "is_pre_order": false,
        "coin_spend_cash_unit": null,
        "attributes": [],
        "stock": 118
    }

as you can see, some fields is combined with annoying "?" I check standar browser request using Fidler, the only thing different from my request is browser send "If-None-Match-" header to the server. question is how to fill that header, because I tried sent etag header from previous request but i still get response with "?" characters.

This is my request code

HttpWebRequest Web;
    HttpWebResponse Resp;
    StreamReader Reader;
    Web = (HttpWebRequest)WebRequest.Create(""https://shopee.co.id/api/v1/item_detail/?item_id=262740376&shop_id=13072340");
    Web.CookieContainer = New CookieContainer();
    Web.Method = "GET";
    Web.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240";
    Web.KeepAlive = true;
    Web.Accept = "application/json";
    Web.ContentType = "application/json; Charset=UTF-8";
    Web.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
    Web.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
    Web.Headers.Add("x-requested-with", "XMLHttpRequest");
    Web.Headers.Add("x-api-source", "pc");

    Resp = (HttpWebResponse)Web.GetResponse();
    Reader = new StreamReader(Resp.GetResponseStream());
    string Res = Reader.ReadToEnd();

this code work for first 5/6 request. after that, server will send annoying "?" like above.

Hope you understand my poor language.

解决方案

anyone please?


这篇关于使用“If-None-Match”标题创建HttpWebRequest(Etag)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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