我一直在HttpClient使用C#进行WebApi调用.似乎比较简洁快捷WebClient.但是我在Https打电话时被困住了.
如何制作以下代码才能Https拨打电话?
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://foobar.com/");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/xml"));
var task = httpClient.PostAsXmlAsync<DeviceRequest>(
"api/SaveData", request);
Run Code Online (Sandbox Code Playgroud)
编辑1: 上面的代码适用于进行http调用.但是,当我将方案更改为https时,它不起作用.这是获得的错误:
底层连接已关闭:无法为SSL/TLS安全通道建立信任关系.
编辑2: 将方案更改为https是:第一步.
如何提供证书和公钥/私钥以及C#请求.
我知道在stackoverflow上有很多与相同主题相关的问题,但在这里我有一些不同的问题:
我用的installer class到reserve a port和bind it with hash上win7/Winxp:
if (Environment.OSVersion.Version.Major > 5)
{
startInfo.Arguments = @"/c netsh http add urlacl url=https://127.0.0.1:8083/ user=EVERYONE";
netsh http add sslcert ipport=127.0.0.1:8083 certhash=df03c4b0b32f3302a3b70abe6b5dfd864d0986a5 appid={00112233-4455-6677-8899-CCBBCCDDEEFF} clientcertnegotiation=enable;
}
else
{
startInfo.Arguments = @"/c httpcfg set urlacl /u https://127.0.0.1:8083/";
httpcfg set ssl -i 127.0.0.1:8083 -h df03c4b0b32f3302a3b70abe6b5dfd864d0986a5 -f 2
}
Run Code Online (Sandbox Code Playgroud)
代码没有问题setup project.一切正常,除了以下几点:
exe上win7它运行WCF成功的web服务.每当我WinXP(SP#)在我的本地机器上使用它时会抛出一个错误:
错误107(net :: ERR_SSL_PROTOCOL_ERROR):SSL协议错误
但是当我使用teamviewer登录任何WinXp虚拟机时,它运行成功.
怎么可能,我试了好几次但得到了同样的结果.它运行在Xp(在VM上运行)但不在真正的XP机器上运行? …
我需要相信应用程序中的一些自签名证书,所以我覆盖了这样的验证回调:
ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
...
public static bool MyRemoteCertificateValidationCallback(
Object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
if (IsAprrovedByMyApplication(sender, certificate)) // <-- no matter what the check here is
return true;
else
return false; // <-- here I'd like to call the default Windwos handler rather than returning 'false'
}
Run Code Online (Sandbox Code Playgroud)
但是当出现一些策略错误,并且我连接的站点未被应用程序批准时,将抛出异常.这里的问题是它与标准的Windows行为不同.
考虑一下这个网站:https://www.dscoduc.com/
它的证书有一个未知的发行者,因此不受信任.我已将它与MMC一起添加到Local Copmuter的Trusted People(它是Windows 7).
如果我在不重写证书验证回调的情况下运行此代码:
HttpWebRequest http = (HttpWebRequest)HttpWebRequest.Create("https://www.dscoduc.com/");
using (WebResponse resp = http.GetResponse())
{
using (StreamReader sr …Run Code Online (Sandbox Code Playgroud) 我需要一些指导.我需要在C#中开发一个可自定义的FTP,它应该使用App.Config文件进行配置.此外,FTP应该再次从任何客户端将数据推送到任何服务器依赖于配置文件.
如果有人可以指导,如果有任何API或任何其他有用的建议,或者让我朝着正确的方向前进,我将不胜感激.
我正在尝试使用System.Net.Mail.SmtpClient类通过我公司的电子邮件服务器中继电子邮件.与邮件服务器的所有SMTP连接都必须是SSL,并且它使用自签名证书.这对于Outlook来说很好,你可以在警告对话框上单击确定,但是有没有人知道让SmtpClient接受自签名证书的方法?
我打算在Windows Azure平台上使用此应用程序,因此我无法将自签名证书安装为受信任的根.
我正在使用HttpWebRequest对象通过HTTP POST访问Web服务。部分要求是我:
HttpWebRequest是否自动为我处理?我假设如果出现这些情况中的任何一个,我将得到标准的“无法为SSL / TLS安全通道建立信任关系”异常。
我会清楚地向您提供我的问题,以便您回答我
我有一个客户端 - 服务器(套接字)连接,我使用SslStream保护,因为我知道使用ssl确保我的客户端只会连接到我的服务器
要做到这一点,我必须向我的客户端添加一个功能来验证服务器认证并确保服务器是真实的(我的服务器)
但我真的不知道如何验证我的自签名认证并希望得到你的帮助
此致,并提前致谢
当我尝试单击一个单独的asp页面WebClient用于从邮政编码下载完整的地址详细信息时,我一直收到错误消息。
The underlying connection was closed: An unexpected error occurred on a send.
Run Code Online (Sandbox Code Playgroud)
这仅在服务器上时发生。不仅如此,当我将URL粘贴到浏览器中时,它可以完美运行。这可能是防火墙设置吗?
这是我正在使用的代码(摘自此处的另一篇文章)
using (CookieAwareWebClient WC = new CookieAwareWebClient())
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;
string data = WC.DownloadString("https://www.myUrl.com/postcode.asp?postcode=" + postcode + "&houseNumber=" + Server.HtmlEncode(txtHouseNumber.Text));
}
Run Code Online (Sandbox Code Playgroud)
最初我只是使用它,结果相同:
WebClient client = new WebClient();
string address = "https://www.myUrl.com/postcode.asp?postcode=" + postcode + "&houseNumber=" + Server.HtmlEncode(txtHouseNumber.Text);
string data = client.DownloadString(address);
Run Code Online (Sandbox Code Playgroud)
该应用程序使用.NET 4 / C#构建,并通过iis6托管在Windows Server 2003上。
如果这是防火墙或安全设置,那会是什么?如果没有,对原因或解决方法有什么想法?非常感谢
更新-########################
好的,我也尝试使用HttpWebRequest,在第二行出现错误:
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(new Uri("https://www.myUrl.com/postcode.asp?postcode=AZ11ZA&houseNumber=1"));
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Run Code Online (Sandbox Code Playgroud)
该错误消息包含:
System.Threading.Thread.AbortInternal() …Run Code Online (Sandbox Code Playgroud) c# ×7
.net ×3
certificate ×3
ssl ×3
asp.net ×1
httpcfg.exe ×1
security ×1
self-signed ×1
smtpclient ×1
sslstream ×1
vb.net ×1
webclient ×1
windows-xp ×1
winforms ×1