2012-12-20 152 views
0

我在本地系統上運行IIS 5.1(在本地主機上)上設置了兩個網站。我打電話給另一個網站。我正在使用ASP.NET,C#2.0。當從另一個本地主機網站調用一個本地主機網站時未經授權(401)

public static String executeWebRequest(string url, Boolean esmRequest) 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
     UTF8Encoding encoding = new UTF8Encoding(); 
     Stream requestStream = null; 
     HttpWebResponse response = null; 
     StreamReader responseStream = null; 
     string responseString; 

     try 
     { 
      //post request 
      request.Method = "POST"; 

      if (esmRequest) 
      { 
       //request.UseDefaultCredentials = true; 
       request.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["ESMServerUserName"], ConfigurationManager.AppSettings["ESMServerPassword"]); 
      } 
      else 
      { 
       //request.UseDefaultCredentials = true; 
       request.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["ESMServerUserName"], ConfigurationManager.AppSettings["ESMServerPassword"]); 
      } 

      requestStream = request.GetRequestStream(); 
      requestStream.Write(new byte[0], 0, 0); 
      requestStream.Close(); 

      //get response 
      response = (HttpWebResponse)request.GetResponse(); 
      responseStream = new StreamReader(response.GetResponseStream(), encoding); 
      responseString = responseStream.ReadToEnd(); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      if (requestStream != null) 
      { 
       requestStream.Close(); 
      } 

      if (response != null) 
      { 
       response.Close(); 
      } 

      if (responseStream != null) 
      { 
       responseStream.Close(); 
      } 
     } 

     return responseString; 
    } 
} 

我運行此代碼並獲取401 HTTP狀態代碼。 1 errorm,而當我在瀏覽器中粘貼相同的URL時,它會完美執行。

我已經使用http://support.microsoft.com/kb/89686/en-us

我該如何解決這使DisableLoopbackCheck?

+0

使用的瀏覽器(如果它的工作)是Internet Explorer中意外解決這個問題? – Najzero

+0

還有一件事我忘了提及我在這兩個網站都有Windows身份驗證。 – Arshya

+0

直接粘貼URL可以在所有三種 - Firefox,IE和Chrome中使用。 – Arshya

回答

0

我能夠與這種變化

var cache = new CredentialCache(); 

NetworkCredential credential = new NetworkCredential(ConfigurationManager.AppSettings["ESMServerUserName"], ConfigurationManager.AppSettings["ESMServerPassword"]); 

cache.Add(new Uri(url), "Basic", credential); 
request.Credentials = cache;  
相關問題