2016-07-12 8 views
2

C#が比較的新しいですが、進歩しています。単純なWebRequestのネットワーク資格情報を設定する

私は現在、System.Net.WebRequestメソッドをテストしようとしています。便利なhttpテストキットをhttps://httpbin.org/で使用しています - ネットワーク資格情報をhttps://httpbin.org/basic-auth/user/passwdに渡し、正常な接続を取得しようとしています。 (現在401を取得中)

ユーザ名はpassword、パスワードはpasswrdです。

私はシンプルなフォームとリクエストを開始するボタンがあります。しかし、述べたように、それは動作していない、と私は401のエラーを取得しています。ここで

私がこれまで持っているものです。問題は、あなたの資格情報である

 private void button1_Click(object sender, EventArgs e) 
    { 

     NetworkCredential myCred = new NetworkCredential(
     "user", "passwrd", "https://httpbin.org"); 


     // Create a request for the URL. 
     WebRequest request = WebRequest.Create(
      "https://httpbin.org/basic-auth/user/passwd"); 
     // If required by the server, set the credentials. 
     request.Credentials = myCred; 
     // Get the response. 
     WebResponse response = request.GetResponse(); 
     // Display the status. 
     Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
     // Get the stream containing content returned by the server. 
     Stream dataStream = response.GetResponseStream(); 
     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader(dataStream); 
     // Read the content. 
     string responseFromServer = reader.ReadToEnd(); 
     // Display the content. 
     Console.WriteLine(responseFromServer); 
     // Clean up the streams and the response. 
     reader.Close(); 
     response.Close(); 
    } 
+1

パスワードを母 'passwd'ない' passwrd' ... – SimpleVar

+0

です!それは良いスタートです! - 私はまだエラーが発生しています。 – bushell

+0

私はドメインがそうであるとは思わない: "https://httpbin.org"。それは "MYDOMAIN \ myusername"のようなものでなければなりません –

答えて

2

、あなたはdomainは、HTTPのドメインであることを想定しているが、それは、Active Directoryドメインのようなもののためにのみ有用です。ちょうどそのパラメータを削除(およびパスワードを修正)し、それが動作します:

NetworkCredential myCred = new NetworkCredential("user", "passwd"); 
関連する問題