110,536
社区成员
发帖
与我相关
我的任务
分享
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;
}
X509CertificateCollection certs = new X509CertificateCollection();
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;
}
X509CertificateCollection certs = new X509CertificateCollection();
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;
}
// 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;
}