我有一段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;
}
}
各位请看一下我翻译的是否正确,这些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;
}
}
解决方案 »
- 页面加载时出现datagridview然后鼠标在别的地方单击时datagridview隐藏
- sql Server 2008 里如何开启'Ad Hoc Distributed Queries' ,服务?我想使用openRowSet功能来把EXCE数据导入数据库,加急,在线等
- 每一小时同一时刻要做同样的事情,如何写语句???
- .NET 程序 编译问题
- 问个关于UDP断口处理的问题
- 数据排列问题(急)
- 关于取随机数时产生的一个百思不得其解的问题!!!
- U.are.U4000b指纹仪C#二次开发问题
- C#新手问题求解决
- 求救:“项目**需要策略管理,请在Visual studio .net企业级开发版或**打开"是什么意思?
- PaintEventArgs,如何绘制 带圈数字
- 使用asp.net身份验证功能来构建网站的成员系统,同一个用户同时多点登录会出现什么问题?
如果还没有解决,查看本地安全策略中,IIS管理器中站点的默认匿名访问帐号或者其所属的组是否有通过网络访问服务器的权限,如果没有:
开始->程序->管理工具->本地安全策略->安全策略->本地策略->用户权限分配,双击“从网络访问此计算机”,添加IIS默认用户或者其所属的组。
NetworkCredential 类是为基于密码的身份验证方案(如基本、简要、NTLM 和 Kerberos)提供凭据的基类。实现 ICredentials 接口的类(如 CredentialCache 类)返回 NetworkCredential 对象。此类不支持基于公钥的身份验证方法,如安全套接字层 (SSL) 客户端身份验证。