Click here to Skip to main content
13,353,124 members (84,169 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 21 Jun 2010

NetworkCredential and Apache

, 23 Jun 2010
Rate this:
Please Sign up or sign in to vote.
When sending an HttpWebRequest to an Apache server with login and password, a 401 error is returned
I had some problem getting connected to an Apache server. It returned a 401, not recognizing my credentials.
Hans Liss had a solution to this problem by manually adding credentials to the message header:

HttpWebRequest req = 
    WebRequest.Create("") as HttpWebRequest;
req.Method = "POST";
req.Timeout = 500;
NetworkCredential cred = new NetworkCredential("user", "pwd");
req.Credentials = cred;

// Fix
string authInfo =
    ((cred.Domain != null) && (cred.Domain.Length > 0) ?
    cred.Domain + @"\" : string.Empty) +
    cred.UserName + ":" + cred.Password;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));

req.Headers["Authorization"] = "Basic " + authInfo;
req.PreAuthenticate = true;
req.ServicePoint.Expect100Continue = false;

This tip was useful for me at least. :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Amund Gjersøe
Norway Norway
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionLogined or not? Pin
source.compiler27-Apr-13 0:57
membersource.compiler27-Apr-13 0:57 
hey my friend!
i have a question, i am trying to connect to myphpadmin with c#, i used many codes to do i. my problem is how recognize i authenticated? status descriptio with true, and false password is OK! i dnt know which property of httpwebresponse will show me authentication state, in other words, how know im logined or not?
i used webbrowser control to show my phpmyadmin panel! but with this code it always show me login page, what wrong here?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 23 Jun 2010
Article Copyright 2010 by Amund Gjersøe
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid