无法从HTTP Post检索服务器端的邮件正文

时间:2010-08-08 18:24:23

标签: c# httpwebrequest

我正在使用HTTPWebRequest向URL发送HTTP post请求。我使用multipart / form-data内容类型以及正文的内容长度发送帖子数据。但是,在服务器端,我无法检索正文。我只能看到发送的标题。我发送的正文的内容长度也匹配。

为什么我无法找回尸体。

请求方法如下所示:

public void Reset(string originalFileData, string uploadLocation)
    {
        TcpClient client = new TcpClient();
        IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(Server), portNo);
        client.Connect(serverEndPoint);
        string responseContent;
        string serverUrl = "http://" + Server + ":" + portNo + "/abc.aspx" + "?uplvar=" + uploadLocation;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUrl);
        request.ContentType = "multipart/form-data";
        request.Method = "POST";
        request.ServicePoint.Expect100Continue = false;
        string postData = originalFileData;
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        response.Close();
    }

编辑:我忘了提及,我能够在第一次发送请求时检索正文,但是在我发送的任何后续请求中,我无法检索它。我每次发送请求时都会创建一个新连接。因此,有些东西可能会阻止检索请求体。我不知道为什么。

1 个答案:

答案 0 :(得分:0)

尝试替换

request.ContentType = "multipart/form-data";

request.ContentType = "application/x-www-form-urlencoded";

或检查此SO answer以查找适用于multipart / formdata的代码。