使用c#调用XMLHTTP(XMLHTTPClass) ,发送和返回 json
时间:2011-03-16 来源:mjg
问题:今天同事编写搜索国外网站搜索代码时 获取不到下一页的数据,使用fiddler后看到如下信息
POST http://www.kalunga.com.br/Busca.aspx/BindSearch HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Referer: http://www.kalunga.com.br/busca/ip/1
x-requested-with: XMLHttpRequest
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; TheWorld)
Host: www.kalunga.com.br
Content-Length: 134
Connection: Keep-Alive
普通的页面
GET http://www.pinnaclemicro.com/computer/parts.php?s=Hard+Drives&elink=megamenu HTTP/1.1
Host: www.pinnaclemicro.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.pinnaclemicro.com/computer/parts.php
Cookie
原因: 不能使用HttpWebRequest 来获取数据。
解决办法:使用XMLHttp获取数据。测试代码如下
string url = "http://www.kalunga.com.br/Busca.aspx/BindSearch";
XMLHTTP r = new XMLHTTP();
r .open ("POST",url ,false ,null ,null );
r .setRequestHeader("Content-Type","application/json");
r .setRequestHeader("Referer", "http://www.kalunga.com.br/busca/ip/1");
r .send ("{\"currPage\":2,\"idClassificacao\":\"0\",\"idGrupo\":\"0\",\"tipoOrdenacao\":\"1\",\"termoBuscado\":\"ip\",\"tipoPesquisa\":\"1\",\"buscaDescricao\":\"false\"}");
string txt = r.responseText;