Reputation: 107
When typing the IP of my Linksys router (Linksys WRT54GS) in a browser, I get a MessageBox saying
"Please enter Username and Password. Server says: Linksys WRT54GS"
I would like to use this message to identify the device in a C# application I am developing. How do I get that last part "Server says: Linksys WRT54GS" into a string?
Upvotes: 2
Views: 292
Reputation: 107
I figured out how to do it thanks to this link: http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponseheader.aspx
The idea is to catch the WebException and match the response statuscode with HttpStatusCode.Unauthorized.
Then you can get what you want with challenge = response.GetResponseHeader("WWW-Authenticate");
Thanks to all who helped
Upvotes: 0
Reputation: 4860
The reason you see this message is because the server likely returned HTTP status code 401 Unauthorized
. As part of that response, the server should be also sending WWW-Authenticate:
header, and IIRC for basic
authentication it provides realm
for which authentication is to be provided. realm
is what you see the browser display in that message. See more info here.
Upvotes: 1
Reputation: 13816
What are you using to make the response? WebClient?
Anyways, you need to read the response headers from the request. The header which contains the string you are looking for is:
"WWW-Authenticate"
and it contains a value similar to this:
"Basic realm="Linksys E4200""
You need to parse the value and read what comes after the "Basic realm=" part.
Upvotes: 2