php翻译至c#的错误
我有一段php的代码示例,需要翻译到C#,我自己翻译后,服务器始终提示401 未经验证,我对php不熟悉
各位请看一下我翻译的是否正确,这些curl中的参数意思我不是很明白。非常感谢!。
//Connection Resource
$resource_uri = 'https://app.test.com/test.xml'
//Setup connection
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $resource_uri);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_FAILONERROR, 0);
//Send request
$result_json = curl_exec($curl);
curl_close($curl);
//Done
print_r($result_json);
我写的c#代码
private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
protected void Page_Load(object sender, EventArgs e)
{
string uri = "https://app.test.com/test.xml";
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
NetworkCredential credential = new NetworkCredential(username, password);
request.Credentials = credential;
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (Exception ex)
{
if (response != null)
{
response.Close();
response = null;
}
Response.Write(ex.Message);
return;
}
}
------解决方案--------------------
控制面板->管理工具->计算机管理->本地用户和组,将IUSR_机器名账号启用。
如果还没有解决,查看本地安全策略中,IIS管理器中站点的默认匿名访问帐号或者其所属的组是否有通过网络访问服务器的权限,如果没有:
开始->程序->管理工具->本地安全策略->安全策略->本地策略->用户权限分配,双击“从网络访问此计算机”,添加IIS默认用户或者其所属的组。
------解决方案--------------------
CURLOPT_SSL_VERIFYPEER FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.
NetworkCredential 类是为基于密码的身份验证方案(如基本、简要、NTLM 和 Kerberos)提供凭据的基类。实现 ICredentials 接口的类(如 CredentialCache 类)返回 NetworkCredential 对象。
此类不支持基于公钥的身份验证方法,如安全套接字层 (SSL) 客户端身份验证。