[英]C# 401 error when send request to REST service
我正在嘗試發送http請求以請求REST服務。 但是,我一直收到401錯誤。 我已經在http標頭中包含了基本的授權信息,但是它仍然收到相同的401錯誤。 誰能幫我檢查我的代碼plz? 謝謝!
public void getAuth(string reqUrl)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUrl);
string userinfo = "test:test";
byte[] byteUserInfo = Encoding.ASCII.GetBytes(userinfo);
string code = Convert.ToBase64String(byteUserInfo);
req.Headers.Add("Authorization", "Basic " + code);
req.Method = "get";
req.ContentType = "application/x-www-form-urlencoded";
req.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Console.WriteLine(response);
}
static void Main(string[] args)
{
Tester2 test = new Tester2();
test.getAuth("http://MyHost/wiki/rest/prototype/1/search/site?query=property");
}
嘗試添加以下兩行:
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.