2014-02-26 24 views
-1

我的網址是 http://www.whatsonindia.com/appi/user?channelgenre=all&context=applicationname%3Dsourcebits%3Bheadendid%3D0&dateselected=0&mode=getTVGuideInfo&pageno=1&responseformat=json&responselanguage=English&starthour=0&totalhrdata=24&userid=-1如何使用C#訪問網頁內容

我試試這個

1.

string [email protected]"http://www.whatsonindia.com/appi/user"; 
WebClient client = new WebClient(); 
NameValueCollection myQueryStringCollection = new NameValueCollection(); 
myQueryStringCollection.Add("channelgenre", "all"); 
myQueryStringCollection.Add("context", "applicationname%3Dsourcebits%3Bheadendid%3D0"); 
myQueryStringCollection.Add("dateselected", "0"); 
myQueryStringCollection.Add("mode", "getTVGuideInfo"); 
myQueryStringCollection.Add("pageno", "1"); 
myQueryStringCollection.Add("responseformat", "json"); 
myQueryStringCollection.Add("responselanguage", "English"); 
myQueryStringCollection.Add("starthour", "0"); 
myQueryStringCollection.Add("totalhrdata", "24"); 
myQueryStringCollection.Add("userid", "-1"); 
client.QueryString = myQueryStringCollection; 

var json = client.DownloadString(url); 

2.

string url="http://www.whatsonindia.com/appi/user?channelgenre=all&context=applicationname%3Dsourcebits%3Bheadendid%3D0&dateselected=0&mode=getTVGuideInfo&pageno=1&responseformat=json&responselanguage=English&starthour=0&totalhrdata=24&userid=-1"; 
WebClient client = new WebClient(); 
var data=client.DownloadString(url); 

既給錯誤:(500)internel服務器錯誤,我解決它?任何人都可以幫我

回答

2

這不是你的代碼被打破。您粘貼的網址已損壞,正在迴應內部服務器錯誤(500)。因此,無論您使用什麼方法嘗試獲取內容,響應都是一樣的。一起來看看:

http://tools.seobook.com/server-header-checker/?page=single&url=http%3A%2F%2Fwww.whatsonindia.com%2Fappi%2Fuser%3Fchannelgenre%3Dall%26context%3Dapplicationname%253Dsourcebits%253Bheadendid%253D0%26dateselected%3D0%26mode%3DgetTVGuideInfo%26pageno%3D1%26responseformat%3Djson%26responselanguage%3DEnglish%26starthour%3D0%26totalhrdata%3D24%26userid%3D-1&useragent=1&typeProtocol=11

服務器響應:HTTP/1.1 500內部服務器錯誤

所以首先運行一些測試之後。看起來你必須先觸摸網址http://www.whatsonindia.com/。第一頁設置了您嘗試訪問的頁面所需的cookie。沒有cookie,你會得到http 500錯誤。

我挖了一些,它看起來像您需要的餅乾是: USERINFO = { 「用戶id」: - 1} _woi-web_session =會議在這裏值

client.Headers.Add(HttpRequestHeader.Cookie, "userInfo={'userid':-1}"); 
client.Headers.Add(HttpRequestHeader.Cookie, "_woi-web_session=[value here]"); 

我使用Firefox和Firebug的插件來查看網站設置的cookie。我會一次刪除一個,直到頁面給了我http 500錯誤,並得出結論,這兩個cookie是頁面工作所必需的。

使用它,它的工作原理我測試了它:它是如何工作的,首先它向地址http://www.whatsonindea.com/發出請求以獲取頁面設置的cookie。然後,我向第一個Web請求傳遞的Cookie指定的網址發出請求。

string baseUrl = "http://www.whatsonindia.com"; 
WebRequest request = HttpWebRequest.Create(baseUrl); 
WebResponse response = request.GetResponse(); 
string cookiesVals = response.Headers[HttpResponseHeader.SetCookie]; 

string url = "http://www.whatsonindia.com/appi/user?channelgenre=all&context=applicationname%3Dsourcebits%3Bheadendid%3D0&dateselected=0&mode=getTVGuideInfo&pageno=1&responseformat=json&responselanguage=English&starthour=0&totalhrdata=24&userid=-1"; 
WebClient client = new WebClient(); 
client.Headers.Add(HttpRequestHeader.Cookie, cookiesVals); 
var data = client.DownloadString(url); 
+0

你能告訴頁面如何設置cookie。請爲此提供代碼 – user3345208