我有以下代码:
string url = "https://myurl.com/is/here/example?param1=100¶m2=200";
string post = "POST " + url + " HTTP/1.1rn" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8rn" +
"Accept-Encoding: gzip, deflatern" +
"Accept-Language: en-US,en;q=0.5rn" +
"Connection: keep-alivern" +
"Host: myurl.comrn" +
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0rn" +
"Content-type: application/jsonrn" +
"X-HTTP-Method-Override: GETrn" +
"X-MY-CUSTOM-HEADER: headervaluernrn";
TcpClient tcp = new TcpClient("myurl.com", 443);
string returnData = string.Empty;
using (SslStream stream = new SslStream(tcp.GetStream()))
{
//Authenticate here...
byte[] data = Encoding.ASCII.GetBytes(post);
stream.Write(data, 0, data.Length);
stream.Flush();
stream.ReadByte();
data = new byte[4096];
var n = stream.Read(data, 0, data.Length);
using (MemoryStream ms = new MemoryStream(data, 0, n))
{
ms.ReadByte();
ms.ReadByte();
using (DeflateStream df = new DeflateStream(ms, CompressionMode.Decompress))
using (StreamReader rd = new StreamReader(df))
{
returnData = rd.ReadToEnd();
}
}
}
tcp.Close();
但是,响应总是空的。帖子字符串正确吗?我在这里缺少什么?
编辑:
我正在使用sslstream,它似乎正在检索数据。但是,是否有一种方法可以读取直到收到所有数据而不是
看来,您正在将内容的长度声明为URL的长度(当表示为UTF-8时),而不是post
Body中的内容
如果内容不如内容长度标头指示的时间,我希望远程服务器等待其余内容。
您也连接到端口443,您的URL开始'https',但您似乎没有尝试进行SSL谈判。