我有以下代码:
string url = "https://myurl.com/is/here/example?param1=100¶m2=200";
string post = "POST " + url + " HTTP/1.1\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
"Accept-Encoding: gzip, deflate\r\n" +
"Accept-Language: en-US,en;q=0.5\r\n" +
"Connection: keep-alive\r\n" +
"Host: myurl.com\r\n" +
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0\r\n" +
"Content-type: application/json\r\n" +
"X-HTTP-Method-Override: GET\r\n" +
"X-MY-CUSTOM-HEADER: headervalue\r\n\r\n";
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,它似乎正在检索数据。但是,有没有办法只读取所有数据,而不是
答案 0 :(得分:0)
您似乎是按照网址的长度(表示为UTF-8)宣布内容的长度,而不是post
正文中的内容
如果内容不是内容长度标题所指示的内容,我希望远程服务器等待剩余的内容。
您还连接到端口443,并且您的网址开始为“https”,但您似乎没有尝试进行SSL协商。