C# 模拟 xmlhttprequest 并获取 json 响应

发布于 2024-12-25 06:18:37 字数 2478 浏览 0 评论 0原文

如何模拟 xmlhttprequest 并获取 json 响应?

此处回答:

HTTP/1.1 200 OK
Server: nginx/1.1.7
Date: Fri, 06 Jan 2012 10:50:36 GMT
Content-Type: application/x-javascript; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding, Accept-Language,Cookie
Content-Language: ru
Content-Encoding: gzip

此处请求:

GET /community/accounts/?type=table&_=1325847040822&offset=0&limit=25&order_by=name&search=Danzanus&echo=2&id=accounts_index HTTP/1.1
Host: worldoftanks.ru
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
X-Requested-With: XMLHttpRequest
X-CSRFToken: aec8c3f844e930e61e8a1b7f2a51b175
Referer: http://worldoftanks.ru/community/accounts/
Cookie: csrftoken=aec8c3f844e930e61e8a1b7f2a51b175; __utma=1.624357332.1325795055.1325812734.1325847014.4; __utmz=1.1325795055.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); csw_popup=true; csw_top=true; __utmb=1.1.10.1325847014; __utmc=1

站点是 http://worldoftanks.com/community/accounts/

GET 请求带参数: http://worldoftanks.ru/community/accounts/?type=table&_=1325847040822&offset=0&limit=25&order_by=name&search=Danzanus&echo=2&id=accounts_index

我需要获取带有用户个人资料链接的搜索结果。 Firebug(FF插件)显示这个答案:

{"request_data":{"items":[{"account_url":"/community/accounts/4213704-Danzanus/","abbreviation":"","exp":514111,"name":"Danzanus","clan_url":"","owner":null,"wins":816,"created_at":"2011-11-29","id":4213704,"battles":1626}],"total_count":0,"filtered_count":1,"offset":0,"echo":2},"result":"success"}

我只需要“account_url”。

此代码返回 200 OK,但答案为空:

 $.get("http://worldoftanks.ru/community/accounts", { type: "table", _: 1325811501451, offset: "0", limit: "25", order_by: "name", search: "Danzanus", echo: "3", id: "accounts_index" },
   function (data) {
       alert("Data Loaded: " + data);}

谢谢。

How emulate xmlhttprequest and get json response?

Here Answer:

HTTP/1.1 200 OK
Server: nginx/1.1.7
Date: Fri, 06 Jan 2012 10:50:36 GMT
Content-Type: application/x-javascript; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding, Accept-Language,Cookie
Content-Language: ru
Content-Encoding: gzip

Here Request:

GET /community/accounts/?type=table&_=1325847040822&offset=0&limit=25&order_by=name&search=Danzanus&echo=2&id=accounts_index HTTP/1.1
Host: worldoftanks.ru
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
X-Requested-With: XMLHttpRequest
X-CSRFToken: aec8c3f844e930e61e8a1b7f2a51b175
Referer: http://worldoftanks.ru/community/accounts/
Cookie: csrftoken=aec8c3f844e930e61e8a1b7f2a51b175; __utma=1.624357332.1325795055.1325812734.1325847014.4; __utmz=1.1325795055.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); csw_popup=true; csw_top=true; __utmb=1.1.10.1325847014; __utmc=1

Site is http://worldoftanks.com/community/accounts/

GET request with parameters:
http://worldoftanks.ru/community/accounts/?type=table&_=1325847040822&offset=0&limit=25&order_by=name&search=Danzanus&echo=2&id=accounts_index

I need get result of search with link to user profile. Firebug(FF plugin) show this answer:

{"request_data":{"items":[{"account_url":"/community/accounts/4213704-Danzanus/","abbreviation":"","exp":514111,"name":"Danzanus","clan_url":"","owner":null,"wins":816,"created_at":"2011-11-29","id":4213704,"battles":1626}],"total_count":0,"filtered_count":1,"offset":0,"echo":2},"result":"success"}

I need only "account_url".

this code return 200 OK but answer is null:

 $.get("http://worldoftanks.ru/community/accounts", { type: "table", _: 1325811501451, offset: "0", limit: "25", order_by: "name", search: "Danzanus", echo: "3", id: "accounts_index" },
   function (data) {
       alert("Data Loaded: " + data);}

Thank You.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷→糖丶微凉 2025-01-01 06:18:37

如果这是 Web 服务返回的响应,您只需解析 XHR 响应(例如在 jQuery onsuccess 方法中,这在 Ajax 调用之后调用)并获取 account_url。
否则,如果您想重新创建响应客户端,您应该创建表示响应的 JS 对象,然后将其转换为 JSON 表示形式。然后,您可以从客户端脚本调用返回 JSON 的方法(在本例中为 getMyObjAsJSON)。

这是一个示例

function getMyObj() {
var firstname = 'Michael'
var lastname = 'Jordan'    
return  { FirstName: firstname, LastName: lastname };}  

function getMyObjAsJSON(){
 var myObj = getMyObj();

    if (myObj == null) {
        alert("obj not created");
        return;
    }

    //it converts the object in its JSON rapresentetion
    var jsonObj = $.toJSON(myObj);
return jsonObj} 

If that is the response returned by the webservice you can just parse the XHR response (in the jQuery onsuccess method for instance, this is called after the Ajax call) and get the account_url.
Otherwise, if you want to recreate the response client side you should create your JS object representing the response and then convert it in it's JSON representation. You can then call the method returning JSON (in this case getMyObjAsJSON) from your client script.

Here's a sample

function getMyObj() {
var firstname = 'Michael'
var lastname = 'Jordan'    
return  { FirstName: firstname, LastName: lastname };}  

function getMyObjAsJSON(){
 var myObj = getMyObj();

    if (myObj == null) {
        alert("obj not created");
        return;
    }

    //it converts the object in its JSON rapresentetion
    var jsonObj = $.toJSON(myObj);
return jsonObj} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文