簡體   English   中英

C# webclient 無法從 https 協議獲得響應

[英]C# webclient cannot getting response from https protocol

當我嘗試通過 https 從服務器加載 html 時,它返回錯誤代碼 500:但是當我在瀏覽器中打開相同的鏈接時它工作正常:有什么辦法嗎? 我正在使用 Webclient 並將用戶代理信息發送到服務器:

HttpWebRequest req1 = (HttpWebRequest)WebRequest.Create("mobile.unibet.com/";); 
req1.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
var response1 = req1.GetResponse();
var responsestream1 = response1.GetResponseStream();

這可能應該是一個評論,但評論中沒有足夠的空間來回答所有問題......我認為這個問題沒有足夠的信息來回答任何程度的自信。

500 錯誤表示服務器出現問題。 簡短的回答是瀏覽器正在發送一些 WebClient 沒有發送的內容。

WebClient 可能未發送服務器預期的標頭。 服務器是否需要身份驗證? 這是您與之簽約的公司的頁面,可能為您提供了憑據或 API 密鑰,您需要添加HTTP 授權嗎?

如果這是您在與您有合作關系的公司做的事情,您應該能夠要求他們幫助追蹤您收到 500 錯誤的原因。 否則,您可能需要向我們提供代碼示例和更多詳細信息,以便我們提供更多建議。

大衛是正確的,這通常發生在服務器期望某些未通過的標頭時,在您的情況下接受

這段代碼現在有效

string requestUrl = "https://mobile.unibet.com/unibet_index.t";
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.UserAgent = "//Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

using (var response = request.GetResponse() as HttpWebResponse)
{
 using (var sr = new StreamReader(response.GetResponseStream()))
 {
           var responsestring = sr.ReadToEnd();
       if (!string.IsNullOrEmpty(responsestring))
       {
    Console.WriteLine(responsestring);
           }
  }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM