我可以使用C#中的HTTP Post将数据包(不是文件)发送到远程服务器吗?如果是这样,该命令将如何显示?如果没有,发送此数据包的最佳方式是什么?该数据包由随机字符数据和2字节标题组成,数据的长度和要执行的操作都是在将其发送到服务器的解决方案内部构建的。我假设我将使用GET命令来获取服务器响应。
答案 0 :(得分:0)
这是一种通用方法 假设您有一个[Data]数据包要发送:
HttpWebRequest httpRequest = WebRequest.CreateHttp("Login Url");
httpRequest.Method = WebRequestMethods.Http.Post;
httpRequest.ContentType = "*The content type, if necessary*";
httpRequest.ContentLength = [Data].Length;
// Other Headers configuration if needed
using (Stream _stream = httpRequest.GetRequestStream())
{
_stream.Write([Data], 0, [Data].Length);
}
using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
{
// Get the response from the server
}