之前用socket发送数据到http地址,主要代码如下
public static string PostMsg(string Domain,int port, string hostUrl, string postData)
{
byte[] tempby = Encoding.UTF8.GetBytes(postData);
string sendStr = String.Format("POST {0} HTTP/1.0\r\n", hostUrl);
sendStr += "Content-Type: application/x-www-form-urlencoded\r\n";
sendStr += "Accept-Language: zh-cn\r\n";
sendStr += "User-Agent: mcj\r\n";
sendStr += "Accept: */*\r\n";
sendStr += "Accept-Encoding: gzip,deflate\r\n";
sendStr += "Host: " + Domain+":"+port + "\r\n";
sendStr += "Content-Length: " + tempby.Length.ToString() + "\r\n";
sendStr += "Connection: Keep-Alive\r\n\r\n"; sendStr += postData;
byte[] bytesSendStr = Encoding.UTF8.GetBytes(sendStr); //将发送内容字符串转换成字节byte数组 bool isIpAddress = false;
try
{
IPAddress ip = IPAddress.Parse(Domain);
isIpAddress = true;
}
catch (Exception)
{
}
ConnectInternet(Domain,port,isIpAddress);
try
{
_socket.Send(bytesSendStr, bytesSendStr.Length, 0);//向主机发送请求
}
catch (Exception ce)
{
//Response.Write(ce.Message + "\r\n");
} string recvStr = ""; //声明接收返回内容的字符串
byte[] recvBytes = new byte[1024];//声明字节数组,一次接收数据的长度为1024字节
int bytes = 0;
while (true)
{
bytes = _socket.Receive(recvBytes, recvBytes.Length, 0); //返回实际接收内容的字节数,循环读取,直到接收完所有数据
if (bytes <= 0)
break;
Encoding gb2312 = Encoding.UTF8;//将读取的字节数转换为字符串
recvStr += gb2312.GetString(recvBytes, 0, bytes);
} recvStr = recvStr.Substring(recvStr.IndexOf("\r\n\r\n") + 4); DisConnectInternet();
return recvStr;
}
现在需要将http地址改成https地址,哪位大侠帮忙解决一下啊。我的联系方式QQ:154874708,邮箱[email protected]
public static string PostMsg(string Domain,int port, string hostUrl, string postData)
{
byte[] tempby = Encoding.UTF8.GetBytes(postData);
string sendStr = String.Format("POST {0} HTTP/1.0\r\n", hostUrl);
sendStr += "Content-Type: application/x-www-form-urlencoded\r\n";
sendStr += "Accept-Language: zh-cn\r\n";
sendStr += "User-Agent: mcj\r\n";
sendStr += "Accept: */*\r\n";
sendStr += "Accept-Encoding: gzip,deflate\r\n";
sendStr += "Host: " + Domain+":"+port + "\r\n";
sendStr += "Content-Length: " + tempby.Length.ToString() + "\r\n";
sendStr += "Connection: Keep-Alive\r\n\r\n"; sendStr += postData;
byte[] bytesSendStr = Encoding.UTF8.GetBytes(sendStr); //将发送内容字符串转换成字节byte数组 bool isIpAddress = false;
try
{
IPAddress ip = IPAddress.Parse(Domain);
isIpAddress = true;
}
catch (Exception)
{
}
ConnectInternet(Domain,port,isIpAddress);
try
{
_socket.Send(bytesSendStr, bytesSendStr.Length, 0);//向主机发送请求
}
catch (Exception ce)
{
//Response.Write(ce.Message + "\r\n");
} string recvStr = ""; //声明接收返回内容的字符串
byte[] recvBytes = new byte[1024];//声明字节数组,一次接收数据的长度为1024字节
int bytes = 0;
while (true)
{
bytes = _socket.Receive(recvBytes, recvBytes.Length, 0); //返回实际接收内容的字节数,循环读取,直到接收完所有数据
if (bytes <= 0)
break;
Encoding gb2312 = Encoding.UTF8;//将读取的字节数转换为字符串
recvStr += gb2312.GetString(recvBytes, 0, bytes);
} recvStr = recvStr.Substring(recvStr.IndexOf("\r\n\r\n") + 4); DisConnectInternet();
return recvStr;
}
现在需要将http地址改成https地址,哪位大侠帮忙解决一下啊。我的联系方式QQ:154874708,邮箱[email protected]
解决方案 »
- 今年的第一个问问,第一个回答的有奖
- DevExpressv9.2源码编译说明
- 紧急求救. sql server2005. 分离.附加等等..
- 关于文本框中文本操作的问题?
- 在MDI窗体中,怎么显示打开文件对话框?
- 从XML中读取数据到DataSet 中速度
- c# webform下,txtbox,dropdownlist失去焦点时,如何激活一个函数(C#函数,非java)!急呀!要多急有多急,大家帮着顶一下!
- 比较怪的问题,有谁帮忙解决谢谢
- 如何得到表的记录数?
- 急!求打包Excel方法!
- DllImport引入DLL文件 找不到指定模块
- SerialPort.DataReceived 事件不起作用
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Threading;
private SslStream mSsl;
private Socket mSocket ; public string PostMsg(string Domain, int port, string hostUrl, string postData)
{
byte[] tempby = Encoding.UTF8.GetBytes(postData);
string sendStr = String.Format("POST {0} HTTP/1.0\r\n", hostUrl);
sendStr += "Content-Type: application/x-www-form-urlencoded\r\n";
sendStr += "Accept-Language: zh-cn\r\n";
sendStr += "User-Agent: mcj\r\n";
sendStr += "Accept: */*\r\n";
sendStr += "Accept-Encoding: gzip,deflate\r\n";
sendStr += "Host: " + Domain + ":" + port + "\r\n";
sendStr += "Content-Length: " + tempby.Length.ToString() + "\r\n";
sendStr += "Connection: Keep-Alive\r\n\r\n"; sendStr += postData;
byte[] bytesSendStr = Encoding.UTF8.GetBytes(sendStr); //将发送内容字符串转换成字节byte数组 bool isIpAddress = false;
try
{
IPAddress ip = IPAddress.Parse(Domain);
isIpAddress = true;
}
catch (Exception)
{
} ConnectInternet(Domain, port, isIpAddress); if (!this.Validate("servername"))
{
return null;
} try
{
if (this.mSsl.CanWrite)
{
this.mSsl.Write(bytesSendStr, bytesSendStr.Length, 0);//向主机发送请求
}
}
catch (Exception ce)
{
//Response.Write(ce.Message + "\r\n");
} string recvStr = ""; //声明接收返回内容的字符串
byte[] recvBytes = new byte[1024];//声明字节数组,一次接收数据的长度为1024字节
int bytes = 0;
while (true)
{
if (this.mSsl.CanRead)
{
bytes = this.mSsl.Read(recvBytes, 0, recvBytes.Length);//返回实际接收内容的字节数,循环读取,直到接收完所有数据
}
if (bytes <= 0)
break;
Encoding gb2312 = Encoding.UTF8;//将读取的字节数转换为字符串
recvStr += gb2312.GetString(recvBytes, 0, bytes);
} recvStr = recvStr.Substring(recvStr.IndexOf("\r\n\r\n") + 4); DisConnectInternet();
return recvStr;
} // The server name must match the name on the server certificate.
private bool Validate(string serverName)
{
// Mark 屏蔽SSL验证
// return true; this.mSsl = new SslStream(new NetworkStream(this.mSocket, FileAccess.ReadWrite, true),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
); this.mSsl.WriteTimeout = 30000;
this.mSsl.ReadTimeout = 30000; try
{
this.mSsl.AuthenticateAsClient(serverName, new X509Certificate2Collection(), SslProtocols.Ssl3, false);
}
catch (AuthenticationException e)
{
string strMsg = string.Format("SSL验证失败!\r\nException: {0}", e.Message);
if (e.InnerException != null)
{
strMsg = string.Format("{1}\r\nInner exception: {0}", e.InnerException.Message, strMsg);
}
Console.WriteLine("Authentication failed - closing the connection.");
this.mSsl.Close();
this.mSocket.Close();
return false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
return true;
}
// 验证证书
private bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors != SslPolicyErrors.None)
{
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
return true;
}
如果不知道servername ,那么参数就传一个空字符串。
然后 替换如下方法
// 验证证书
private bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateNotAvailable)
{
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
return true;
}
private bool Validate(string serverName)
{
// Mark 屏蔽SSL验证
// return true; this.mSsl = new SslStream(new NetworkStream(this.mSocket, FileAccess.ReadWrite, true),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
); this.mSsl.WriteTimeout = 30000;
this.mSsl.ReadTimeout = 30000; try
{
this.mSsl.AuthenticateAsClient(serverName, new X509Certificate2Collection(), SslProtocols.Ssl3, false);
}
catch (AuthenticationException e)
{
string strMsg = string.Format("SSL验证失败!\r\nException: {0}", e.Message);
if (e.InnerException != null)
{
strMsg = string.Format("{1}\r\nInner exception: {0}", e.InnerException.Message, strMsg);
}
Console.WriteLine("Authentication failed - closing the connection.");
this.mSsl.Close();
this.mSocket.Close();
return false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
return true;
}上面有的
X509Certificate cert = X509Certificate.CreateFromCertFile(@"D:\cashcer.cer");
certs.Add(cert);
try
{
sslStream.AuthenticateAsClient("MyServer", certs, SslProtocols.Tls, false);
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
return;
}
再具体的我也帮不了了。
X509Certificate cert = X509Certificate.CreateFromCertFile(@"D:\cashcer.cer");
certs.Add(cert);
try
{
sslStream.AuthenticateAsClient("MyServer", certs, SslProtocols.Tls, false);
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
return;
}这里不是嘛?
不是为了分数来的。SSL这我也是刚学会的、只是顺手帮一下而已。
如果现在不需要客服端 的证书