2013-05-14 32 views
9

Tôi đang cố thực hiện yêu cầu web qua url https với xác thực cơ bản. Và nó không hoạt động! dưới đây là mã của tôi, nó thực sự hoạt động nếu tôi sử dụng một url không an toàn so với một an toàn, và tôi không thể tìm ra những gì tôi đang làm sai. Hoạt động chỉ tìm thấy không an toàn, nhưng khi một url an toàn được sử dụng, tôi nhận được lỗi xác thực người dùng 401. Có thể ai đó đã thiết lập sai trên máy chủ hay là mã của tôi?C# - http yêu cầu web với https và xác thực cơ bản

Ai đó có thể giúp tôi không?

 var req = System.Net.HttpWebRequest.Create(Url) as HttpWebRequest; 
     req.Method = Method.ToString(); 
     req.ContentType = "application/json"; 
     req.Date = RequestTime; 
     req.Proxy = null; 
     string credentials = String.Format("{0}:{1}", "xxxx", "xxxx"); 
     byte[] bytes = Encoding.ASCII.GetBytes(credentials); 
     string base64 = Convert.ToBase64String(bytes); 
     string authorization = String.Concat("Basic ", base64); 
     req.Headers.Add("Authorization", authorization); 
     HttpWebResponse response = (HttpWebResponse)req.GetResponse(); 
    Stream receiveStream = response.GetResponseStream(); 

     StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); 
     string responsebody = readStream.ReadToEnd(); 
     Console.WriteLine(responsebody); 

     response.Close(); 
     readStream.Close(); 
+0

tốt mã có vẻ tốt đẹp để xác thực, nhưng thực sự không thể nói bất cứ điều gì cho đến khi tôi nhìn thấy url. –

+0

Bạn có thể điền thông tin đăng nhập cho kết nối an toàn. http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.credentials.aspx – JuStDaN

+1

Cảm ơn các bạn đã đề xuất. Có vẻ như mã của tôi đã ổn. Họ đã thiết lập một cái gì đó sai trên máy chủ, mà cuối cùng họ đã cố định! – user1096865

Trả lời

11

này làm việc cho tôi:

var webRequest = (HttpWebRequest)WebRequest.Create(url); 
webRequest.Method = "GET"; 
webRequest.ContentType = "application/json"; 
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:28.0) Gecko/20100101 Firefox/28.0"; 
webRequest.ContentLength = 0; // added per comment 
string autorization = "username" + ":" + "Password"; 
byte[] binaryAuthorization = System.Text.Encoding.UTF8.GetBytes(autorization); 
autorization = Convert.ToBase64String(binaryAuthorization); 
autorization = "Basic " + autorization; 
webRequest.Headers.Add("AUTHORIZATION", autorization); 
var webResponse = (HttpWebResponse)webRequest.GetResponse(); 
if (webResponse.StatusCode != HttpStatusCode.OK) Console.WriteLine("{0}",webResponse.Headers); 
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream())) 
{ 
    string s = reader.ReadToEnd(); 
    Console.WriteLine(s); 
    reader.Close(); 
} 
+0

nop, điều này không hoạt động – Cheese

+0

@Cheese Thông báo lỗi với việc thực thi của bạn là gì? –

+0

@Cheese Điều này làm việc cho tôi, tuy nhiên tôi đã phải thêm webRequest.ContentLength = 0 vì nó đã không được hạnh phúc mà không có nó mặc dù nội dung cơ thể là 0. – John

Các vấn đề liên quan