Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WCF Web服务连接测试实用程序_Wcf_C# 4.0_Authentication - Fatal编程技术网

WCF Web服务连接测试实用程序

WCF Web服务连接测试实用程序,wcf,c#-4.0,authentication,Wcf,C# 4.0,Authentication,我正在创建一个可用于更改web服务配置的配置实用程序。我这样做是因为我的应用程序有很多EXE,并且包含很多配置文件 这还用于更改web服务的配置,因为有很多服务,我需要一种简单的方法通过实用程序更改连接字符串中的服务器和虚拟目录 因此,我尝试使用服务url并尝试连接到它,以检查是否可以在不需要创建代理的情况下建立连接 到目前为止,这就是我正在做的: string url = "http://localhost/VirtualDirectory/Service.svc"; HttpWebReque

我正在创建一个可用于更改web服务配置的配置实用程序。我这样做是因为我的应用程序有很多EXE,并且包含很多配置文件

这还用于更改web服务的配置,因为有很多服务,我需要一种简单的方法通过实用程序更改连接字符串中的服务器和虚拟目录

因此,我尝试使用服务url并尝试连接到它,以检查是否可以在不需要创建代理的情况下建立连接

到目前为止,这就是我正在做的:

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!!!"); }
}
但是我得到了“远程服务器返回了一个错误:(401)未经授权。”,异常状态为“System.Net.WebExceptionStatus.ProtocolError”

但是,当我试图使用这些来自浏览器的凭据连接到web服务时,我能够连接并查看服务xml

我不确定是我做错了什么,还是IIS方面有什么问题(IIS 7.5)。我看到,对于该服务,在IIS中启用了匿名身份验证,禁用了rest

我无法从另一个问题中得到答案。但它可能是一个复制品。请在这种情况下提供链接


谢谢。

我解决了这个问题和解决方案。我没有意识到服务需要一个我没有提供的“授权”自定义头,因为我没有注意到它。我的错

下面是对我有用的代码:

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);
        }
    }
}

为什么不使用channel factory来使用您的服务?您有webHttpBinding端点吗?我不想使用ChannelFactory的原因是因为我不想使用该服务。我只想确保用户输入的新服务器名和虚拟目录正确无误,并且可以建立连接。我不想做太多的工作,只是建立一个连接并关闭它。