2016-09-21 8 views
0

私はWCFウェブサイトを使用しようとしていますが、これはsiteminderで保護されています。問題は、ブラウザでWebサービスのURLを参照しようとしているときに、私が提供している認証情報で正常に動作していることです。WCFサービスでsiteminderが保護されている。C#

しかし、プログラムで同じことをしようとすると、エラー# #401が不正になりました。参照用

- http://www.codeproject.com/Articles/80314/How-to-Connect-to-a-SiteMinder-Protected-Resource

 CookieContainer cookies = null; 
     HttpWebRequest request = null; 
     HttpWebResponse response = null; 
     string responseString = null; 
     NameValueCollection tags = null; 
     string url = null; 
     url = PROTECTED_URL; 
     Debug.WriteLine("Step 1: Requesting page @" + url); 
     request = (HttpWebRequest)WebRequest.Create(url); 
     request.AllowAutoRedirect = false; 
     response = (HttpWebResponse)request.GetResponse(); 
     ShowResponse(response); 
     // Step 2: Get the redirection location 
     // make sure we have a valid response 
     if (response.StatusCode != HttpStatusCode.Found) 
     { 
      throw new ApplicationException(); 
     } 
     url = response.Headers["Location"]; 
     // Step 3: Open a connection to the redirect and load the login form, 
     // from this screen we will capture the required form fields. 
     Debug.WriteLine("Step 3: Requesting page @" + url); 
     request = (HttpWebRequest)WebRequest.Create(url); 
     request.AllowAutoRedirect = false; 

     try 
     { 
      response = (HttpWebResponse)request.GetResponse(); 
     } 
     catch (Exception ex) 
     { 
      string str = ex.Message.ToString(); 
     } 
+0

したがって、ブラウザからサンプルメソッドを呼び出すと、正常に動作しますが、同じメソッドをプログラムで呼び出すと、正しく動作しません。最初にコードを表示します。 –

+0

私は自分の資格情報でURLをヒットしたときに問題なく動作していますが、プログラムで応答を得ようとしているときにエラー –

+0

が表示されます。どのようにそれを取得しようとする –

答えて

0

それは、WCFを呼び出すために私HtTpClientです。

public async Task<string> webClient(string method, string uri) 
     { 
       try 
       { 

        var client = new HttpClient(); 
        client.Timeout =new TimeSpan(0,0,0,10); 
        client.BaseAddress = new Uri(uri); 
        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json")); 
        var response = client.GetAsync(method).Result; 

        string content = await response.Content.ReadAsStringAsync(); 
        return content; 

       } 
       catch (Exception ex) 
       { 
        return "Error"; 
       } 
} 

Uriはベースアドレスです。メソッドはメソッド名です。

string response = webClient(uri + "/GetSomething/", uri).Result; 
+0

あなたは完全なコードを共有することができます –

関連する問題