Hon*_*ner 12 c# https certificate http-post httpwebrequest
在我的C#Windows客户端中,我有一个POST提交给"母舰".当然,我希望提交的数据是安全的,所以我付了HostGator的费用来发给我SSL证书.
我保存了.CER文件,我正在构建请求:
//wrapper for WebClient object to use certificate file
class SecureWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
string certPath = @"e:\mycertificate.cer";
X509Certificate myCert = X509Certificate.CreateFromCertFile(certPath);
request.ClientCertificates.Add(myCert);
return request;
}
}
//request
private static SecureWebClient client = new SecureWebClient();
private static NameValueCollection = new NameValueCollection();
nvc.Add(POST_ACTION, ACTION_CODE_LOGIN);
nvc.Add(POST_EMAIL, email);
nvc.Add(POST_PASSWORD, password);
sResponse = System.Text.Encoding.ASCII.GetString(client.UploadValues(BASE_URL + ACTION_PAGE, nvc));
Run Code Online (Sandbox Code Playgroud)
它抛出一个System.Net.WebException:
底层连接已关闭:发送时发生意外错误.
InnerException是一个System.IO.IOException:
由于意外的数据包格式,握手失败.
对我做错了什么的见解?
pou*_*pou 11
如果您没有使用客户端证书,并且可以使用https://访问您的服务器,那么您的代码应如下所示:
private static WebClient client = new WebClient();
private static NameValueCollection nvc= new NameValueCollection();
nvc.Add(POST_ACTION, ACTION_CODE_LOGIN);
nvc.Add(POST_EMAIL, email);
nvc.Add(POST_PASSWORD, password);
sResponse = System.Text.Encoding.ASCII.GetString(client.UploadValues(BASE_URL + ACTION_PAGE, nvc));
Run Code Online (Sandbox Code Playgroud)
只要您的BASE_URL使用https://,就会加密所有数据(进出服务器).
IOW使用SSL/TLS从客户端到服务器不要求客户端做任何特殊的事情(使用证书),除了使用https://作为方案,因为操作系统提供您需要的所有内容(例如,受信任的根)保护数据传输.
归档时间: |
|
查看次数: |
24960 次 |
最近记录: |