在Windows窗体应用程序解决方案中添加WCF引用时出错

时间:2015-09-24 08:07:18

标签: c# .net wcf

元数据包含无法解析的引用:' http://192.168.1.86:8080/spectrum/restful/models'。 HTTP请求未经授权使用客户端身份验证方案' Anonymous'。从服务器接收的认证标题是“基本领域="频谱"'”。 远程服务器返回错误:(401)未经授权。 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

This is the AUnthencation PopUp enter image description here Error That is comming while adding the reference in the windows solution

请帮助!!

1 个答案:

答案 0 :(得分:0)

string res = string.Empty;

        string url = "http://192.168.1.86:8080/spectrum/restful/models";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Credentials = CredentialCache.DefaultCredentials;
        string authInfo = userName + ":" + password;
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
        request.Headers["Authorization"] = "Basic " + authInfo;
        HttpWebResponse response = null;
        try
        {
            response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            HttpStatusCode errorCode = response.StatusCode;
            reader.Close();
            dataStream.Close();
            response.Close();
            res = responseFromServer;
        }
        catch (Exception ex)
        {
            if (response == null && ex.Message.Contains("400"))
                res = "NoSuchUser";
            else
                res = ex.Message;
        }