Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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
C# ';未经授权';用C开发的.net Web服务出错#_C#_.net_Asp.net - Fatal编程技术网

C# ';未经授权';用C开发的.net Web服务出错#

C# ';未经授权';用C开发的.net Web服务出错#,c#,.net,asp.net,C#,.net,Asp.net,请求失败,HTTP状态为401:未经授权。为什么? TravelCodesTranslator ws = GetEncodeDecodeWS(); // Create and set up the credentials for XmlSelectWebService string UserName = "username"; string Password = "pwd"; // Xml Select uses Basic Authentication NetworkCredential

请求失败,HTTP状态为401:未经授权。为什么?

TravelCodesTranslator ws = GetEncodeDecodeWS();

// Create and set up the credentials for XmlSelectWebService
 string UserName = "username";
 string Password = "pwd";
// Xml Select uses Basic Authentication
NetworkCredential credentials = new NetworkCredential(UserName, Password);
CredentialCache cc = new CredentialCache();
ws.PreAuthenticate = true;
ws.Credentials = credentials;

XmlElement response;

if (_requestType == "Encode")
{
    response = ws.Encode(request.DocumentElement); // got error of
                                                   // "The request failed
                                                   //  with HTTP status 401:
                                                   //  Unauthorized."
}
else
{
    response = ws.Decode(request.DocumentElement);
}
改变

ws.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;


提示:错误信息非常清楚。上面说你的证件不正确。然后我会检查用户名和密码是否正确。如果它们是正确的,那么我将查看它们是否正确分配给请求。

再次编辑您的问题以删除用户名和pwd。请不要在公共场所发布敏感数据。您确定pwd/用户名正确吗。如果是这样,请尝试添加域。
ws.Credentials = credentials