无法从httpclient帖子中获取值

时间:2019-08-24 00:30:54

标签: c# asp.net

我对httpclient有一个疑问。 在本地环境(IIS7.5)中,代码可以工作,但是在服务器(IIS6.0)中,它无法接收数据。我使用webclient,双方都可以正常工作。 我不知道出了什么问题。

服务器端:

string eventIndex = context.Request["event_index"];
        string cardNum = context.Request["card_num"];
        string machineSn = context.Request["machine_sn"];
        string recordType = context.Request["record_type"];
        string doorNum = context.Request["door_num"];
        string inOrOut = context.Request["in_or_out"];
        string isValid = context.Request["is_valid"];
        string eventDate = context.Request["event_date"];
        string eventDes = context.Request["event_des"];
        string token = context.Request["token"];
        string photo = null;

        Javis.WriteLog("Form-Data:[event_index]:" + eventIndex + ";" +
                       "[card_num]:" + cardNum + ";" +
                       "[machine_sn]:" + machineSn + ";" +
                       "[record_type]:" + recordType + ";" +
                       "[door_num]:" + doorNum + ";" +
                       "[in_or_out]:" + inOrOut + ";" +
                       "[is_valid]:" + isValid + ";" +
                       "[event_date]:" + eventDate + ";" +
                       "[event_des]:" + eventDes + ";" +
                       "[token]:" + token + ";");
       //No data received!

        if (string.IsNullOrEmpty(eventIndex) ||
            string.IsNullOrEmpty(cardNum) ||
            string.IsNullOrEmpty(machineSn) ||
            string.IsNullOrEmpty(recordType) ||
            string.IsNullOrEmpty(doorNum) ||
            string.IsNullOrEmpty(inOrOut) ||
            string.IsNullOrEmpty(isValid) ||
            string.IsNullOrEmpty(eventDate) ||
            string.IsNullOrEmpty(eventDes) ||
            string.IsNullOrEmpty(token))
        {
            context.Response.Write("data is empty");
            return;
        }
        //Next, perform the database insert operation

客户端:

using (HttpClient client = new HttpClient())
            {
                //HttpClient client = new HttpClient();
                client.MaxResponseContentBufferSize = 256000;
                client.DefaultRequestHeaders.Add("user-agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
                using (MultipartFormDataContent content = new MultipartFormDataContent(DateTime.Now.Ticks.ToString("X")))
                {
                    content.Add(new StringContent(Convert.ToString(eventIndex)), "event_index");
                    content.Add(new StringContent(carNum), "card_num");
                    content.Add(new StringContent(Convert.ToString(sn)), "machine_sn");
                    content.Add(new StringContent(Convert.ToString(recordType)), "record_type");
                    content.Add(new StringContent(doorNum), "door_num");
                    content.Add(new StringContent(Convert.ToString(inOrOut)), "in_or_out");
                    content.Add(new StringContent(Convert.ToString(valid)), "is_valid");
                    content.Add(new StringContent(Convert.ToString(eventDate)), "event_date");
                    content.Add(new StringContent(Convert.ToString(des)), "event_des");
                    content.Add(new StringContent(Convert.ToString(photoName)), "photo");
                    content.Add(new StringContent(Convert.ToString(Token)), "token");
                    var response = await client.PostAsync(url, content);
                    return await response.Content.ReadAsStringAsync();
                }
            }

服务器接收到的每个字段的值为空

0 个答案:

没有答案