How to pass Network Credentials via WebRequest Object in the using System.Net NameSpace

Discussion in 'ASP.NET / ASP.NET Core' started by Erikkl2000, Aug 17, 2006.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Thanks for the quick reply.....


    Did a quick check to make sure that pass and username is correct... and all is good...


    Here is the error that i recieve now...

    -----------------
    An unhandled exception of type 'System.Net.WebException' occurred in System.dll
    Additional information: The remote server returned an error: (405) Method Not Allowed.
    -----------------


    ============================================



    NetworkCredential nc = new NetworkCredential();


    nc.Password = "wow123";


    nc.UserName = "wow123";


    request.Credentials = nc;





    =============================================











    private void button1_Click(object sender, EventArgs e)


    {


    // Create a request using a URL that can receive a post.


    WebRequest request = WebRequest.Create("http://www.constructionsupercenter.com/Visio/ConstructionSuperCenter.htm");





    NetworkCredential nc = new NetworkCredential();


    nc.Password = "wow123";


    nc.UserName = "wow123";


    request.Credentials = nc;





    // Set the Method property of the request to POST.


    request.Method = "POST";


    // Create POST data and convert it to a byte array.


    string postData = "This is a test that posts this string to a Web server.";


    byte[] byteArray = Encoding.UTF8.GetBytes(postData);


    // Set the ContentType property of the WebRequest.


    request.ContentType = "application/x-www-form-urlencoded";


    // Set the ContentLength property of the WebRequest.


    request.ContentLength = byteArray.Length;


    // Get the request stream.


    Stream dataStream = request.GetRequestStream();


    // Write the data to the request stream.


    dataStream.Write(byteArray, 0, byteArray.Length);


    // Close the Stream object.


    dataStream.Close();


    // Get the response.


    WebResponse response = request.GetResponse();


    // Display the status.


    Console.WriteLine(((HttpWebResponse)response).StatusDescription);


    // Get the stream containing content returned by the server.


    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.


    reader.Close();


    dataStream.Close();


    response.Close();


    }




    http://www.ErikLittle.com
    http://www.StarterSolutions.com
    http://www.BidWar.com
    http://www.constructionsupercenter.com
    http://www.BuildersStop.com
    http://www.ContractorsStop.com
    http://www.ProjectMaterials.com
    http://www.RehabMaterials.com
    http://www.RehabCafe.com
    http://www.SuperCenterPages.com
    http://www.SuperPagesDirectory.com
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    why are you posting to an HTML page?


    switch the method to get


    request.Method = "POST"; ----> request.Method = "GET";

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. There is a possibility that in your case the htm file type is not registered in the IIS script map settings.In IIS 5.1HTTP requests of type POST, HEAD, and all others are responded to with a 405methodnot allowed error.




    Vikram

    DiscountASP.NET
    www.DiscountASP.NET
     
  4. Sorry for the late replay; i have been at ms events today...

    Yes it was because i had set the request method to post and not get... oop's

    I am just now easeing my way into the world of ftp.. (as in my own custom ftp win app)


    Thanks a lot again for your help..


    Erik

    http://www.ErikLittle.com
    http://www.StarterSolutions.com
    http://www.BidWar.com
    http://www.constructionsupercenter.com
    http://www.BuildersStop.com
    http://www.ContractorsStop.com
    http://www.ProjectMaterials.com
    http://www.RehabMaterials.com
    http://www.RehabCafe.com
    http://www.SuperCenterPages.com
    http://www.SuperPagesDirectory.com
     
  5. Hi all,

    I am getting a 401 server error not opening the door for me; can someone please tell me what i need to do to gain access to a password protected directory in my domain?

    Here is the code:




    NetworkCredential nc = new NetworkCredential();


    nc.Password = "xxxPass";


    nc.UserName = "xxxUserName";


    nc.Domain = "http://www.constructionsupercenter.com";


    request.Credentials = nc;





    -------------------------------








    Here is all of the code:





    private void button1_Click(object sender, EventArgs e)


    {


    // Create a request using a URL that can receive a post.


    WebRequest request = WebRequest.Create("http://www.constructionsupercenter.com/Visio/ConstructionSuperCenter.htm");





    NetworkCredential nc = new NetworkCredential();


    nc.Password = "xxxPass";


    nc.UserName = "xxxUserName";


    nc.Domain = "http://www.constructionsupercenter.com";


    request.Credentials = nc;





    // Set the Method property of the request to POST.


    request.Method = "POST";


    // Create POST data and convert it to a byte array.


    string postData = "This is a test that posts this string to a Web server.";


    byte[] byteArray = Encoding.UTF8.GetBytes(postData);


    // Set the ContentType property of the WebRequest.


    request.ContentType = "application/x-www-form-urlencoded";


    // Set the ContentLength property of the WebRequest.


    request.ContentLength = byteArray.Length;


    // Get the request stream.


    Stream dataStream = request.GetRequestStream();


    // Write the data to the request stream.


    dataStream.Write(byteArray, 0, byteArray.Length);


    // Close the Stream object.


    dataStream.Close();


    // Get the response.


    WebResponse response = request.GetResponse();


    // Display the status.


    Console.WriteLine(((HttpWebResponse)response).StatusDescription);


    // Get the stream containing content returned by the server.


    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.


    reader.Close();


    dataStream.Close();


    response.Close();


    }
     
  6. Assuming that the username and password are correct, don't specify the Domain property for the NetworkCredential object. The domain refers to the network domain and not the domain name, so it's usually not needed for basic authentication.

    Aristotle

    DiscountASP.NET
    www.DiscountASP.NET
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page