需要在c#中使用Socket连接类实现SSL才能获取网页。我在谷歌搜索但没有找到任何正确的代码片段 我能够在没有ssl的情况下获取数据
string request = "GET /search?q=devendra&gws_rd=cr&ei=sBkRV4JjxfS6BKP9ieAN HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
// Create a socket connection with the specified server and port.
Socket s = ConnectSocket(server, port);
if (s == null)
return ("Connection failed");
NetworkStream nStream = new NetworkStream(s, true);
// Wrap in SSL stream
SslStream ssStream = new SslStream(nStream);
ssStream.AuthenticateAsClient(server);
// Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0);
// Receive the server home page content.
int bytes = 0;
string page = "Default HTML page on " + server + ":\r\n";
// The following will block until te page is transmitted.
do
{
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);
return page;
在SSLStream的AuthenticateClient方法上获取错误:
由于意外的数据包格式,握手失败。