Reputation: 2533
I am creating a configuration utility that can be used to change the web service configuration. I am doing this as my application has a lot of exes and contains a lot of configuration files.
This is also used to change the configuration for the web service as there are a lot of services and I need and easy way to change the server and the virtual directory in the connection string through a utility.
So what I am trying to do is use the service url and try connect to it to check if the connection can be established without needing to create a proxy.
So far this is what I am doing:
string url = "http://localhost/VirtualDirectory/Service.svc";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Credentials = new NetworkCredential("UserName", "Password");
using(HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode == HttpStatusCode.OK)
{ MessageBox.Show("Connection successful!"); }
else
{ MessageBox.Show("Connection failed!!!"); }
}
But I am getting "The remote server returned an error: (401) Unauthorized." and the exception status is "System.Net.WebExceptionStatus.ProtocolError"
But when I am trying to connect to the web service using these credentials from the browser, I am able to connect and see the service xml.
I am not sure if it is something that I am doing wrong or if there is anything that is wrong from the IIS side(IIS 7.5). I see that for the service, anonymous authentication is enabled and rest is disabled in IIS.
I could not get the solution from another SO question. But it can be a possible duplicate. Please provide a link in such a case.
Thanks.
Upvotes: 0
Views: 1760
Reputation: 2533
I figured out the problem and the solution as well for this. What I did not realize that the service was expecting an "Authorization" custom header which I was not providing, as I didn't notice it. My bad.
Below is the code that worked for me:
string url = @"http://" + ServerName + @"/" + VirtualDirectoryName
+ @"/Service.svc";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
NetworkCredential nc = new NetworkCredential("Username", "Password");
request.Credentials = nc;
string credentials = "Username:Password";
request.Headers.Add("Authorization", "Basic "
+ Convert.ToBase64String(Encoding.Default.GetBytes(credentials)));
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(url), "Basic", nc);
request.Credentials = cache;
request.PreAuthenticate = true;
bool pingSuccess = false;
var ping = new Ping();
PingReply reply;
try
{
reply = ping.Send(ServerName);
if (reply.Status == IPStatus.Success)
pingSuccess = true;
}
catch
{
string msg = "Connection to the server failed.\nCheck server name.";
MessageBox.Show(msg, _error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode == HttpStatusCode.OK)
{
MessageBox.Show("Server connection successful!",
_info,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
if (pingSuccess)
{
string msg = "Server is Available but connection failed."
+ "\nCheck virtual directory name.";
MessageBox.Show(msg,
_error,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
Upvotes: 1