2017-04-26 63 views
0

我有像這樣運行的API:爲什麼curl無法獲得與ie瀏覽器相同的響應?

result in ie browser

,所以我想用curl得到的結果在我的ubuntu的機器,那我試試這個:

curl -v \ 
--header 'Accept: text/html, application/xhtml+xml, */*' \ 
--header 'Accept-Language: zh-CN' \ 
-A 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko' \ 
--header 'Accept-Encoding: gzip, deflate' \ 
--header 'Host: 10.202.15.197:20176' \ 
--header 'DNT: 1' \ 
--header 'Connection: Keep-Alive' \ 
http://10.202.15.197:20176?user_id=1&query_type=GEOSPLIT&address=廣東省深圳市寶安&ret_splitinfo=1 

result with curl

然後發生了奇怪的事情:正如你所看到的,我得到了完全不同的結果,即browswer,所以我想這一定是編碼的問題,那我試試這個:

curl -v \ 
--header 'Accept: text/html, application/xhtml+xml, */*' \ 
--header 'Accept-Language: zh-CN' \ 
-A 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko' \ 
--header 'Accept-Encoding: gzip, deflate' \ 
--header 'Host: 10.202.15.197:20176' \ 
--header 'DNT: 1' \ 
--header 'Connection: Keep-Alive' \ 
http://10.202.15.197:20176 --data-urlencode 'user_id=1&query_type=GEOSPLIT&address=廣東省深圳市寶安&ret_splitinfo=1' 

I get total different result

,但沒有,它返回相同的結果,我通過fiddler趕上我的請求,我的窗口,即瀏覽器,我得到的請求數據:

GET http://10.202.15.197:20176/?user_id=1&query_type=GEOSPLIT&address=廣東省深圳市寶安&ret_splitinfo=1 HTTP/1.1 
Accept: text/html, application/xhtml+xml, */* 
Accept-Language: zh-CN 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko 
Accept-Encoding: gzip, deflate 
Host: 10.202.15.197:20176 
DNT: 1 
Connection: Keep-Alive 
Pragma: no-cache 


HTTP/1.0 200 OK 
Content-Type: application/octet-stream 
Connection: close 
Content-Length: 222 

<?xml version='1.0' encoding='GBK'?> 
<addrSplitInfo> 
<status>0</status><as_info prop="1" level="1">廣東省</as_info> 
<as_info prop="1" level="2">深圳市</as_info> 
<as_info prop="3" level="18">寶安</as_info> 
</addrSplitInfo> 
+0

嗨,提示:菲德勒使用代理服務器連接....關閉您的代理在IE的網絡設置。 –

回答

0

我做很多深入研究這一現象(見here)。原因是IE browser將使用GBK作爲默認編碼,同時ChromeFirefoxcURLpython-requests只使用UTF-8編碼。

下面是使用這個API解決方案:

cURL

echo "http://10.202.15.197:20176\?user_id\=1\&query_type\=GEOSPLIT\&address\=廣東省深圳市寶安\&ret_splitinfo\=1" | iconv -f utf-8 -t gbk | xargs curl 

python-requests

payload = {"user_id": 1, "query_type": "GEOSPLIT", "address": u"廣東省深圳市寶安".encode('gbk'), "ret_splitinfo": 1} 
r = requests.get("http://10.202.15.197:20176", payload) 
print r.text 
相關問題