network Authentication and website Authentication using HttpWebRequest

前端 未结 2 1477
北荒
北荒 2020-12-21 18:47

I am trying to create a application that will consume RSS data using .NET Framework. The RSS site requires User name and Password to start with. and Am running this applicat

相关标签:
2条回答
  • 2020-12-21 19:24

    If this code works, then your original code above was wrong. You should set

    request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
    

    and

    NetworkCredential nc = new NetworkCredential("SITEUSERNAME", "SITEPASSWORD");
    CredentialCache cache = new CredentialCache();
    cache.Add(new Uri(RSSFeed), "Basic", nc);
    cache.Add(new Uri(RSSFeed), "Ntlm", new NetworkCredential("USERNAME","PASSWORD","DOAMIN"));
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
    myHttpWebRequest.Credentials = cache;
    

    In other words, you had exchanged credentials for the proxy and the destination server.

    0 讨论(0)
  • 2020-12-21 19:32

    solved the issue by using following code

     string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("UserName" + ":" + "Password"));
     StringBuilder outputData = new StringBuilder();
     HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
     myHttpWebRequest.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
     myHttpWebRequest.Headers.Add("Authorization", "Basic " + credentials);
     HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
     Stream streamResponse = myHttpWebResponse.GetResponseStream();
    
    0 讨论(0)
提交回复
热议问题