【问题标题】:JSON request is null when POSTing with WebClient but working with WebRequest, HttpClient使用 WebClient 发布但使用 WebRequest、HttpClient 时 JSON 请求为空
【发布时间】:2018-02-09 09:42:44
【问题描述】:

我开发了一个具有以下操作的 Web API 2 应用程序

[Route("submit")]
        [HttpPost]
        public async Task<IHttpActionResult> Submit([FromBody]Request request)
        {
            if (request != null)
            {
                try
                {
                    //Do Something
                }
                catch (Exception ex)
                {
                    return this.InternalServerError(ex);
                }
            }
            else
            {
                return this.BadRequest("Request is null");
            }
        }

在我的客户端应用程序中,如果我使用 WebClient 调用此操作,则操作中的请求对象将变为 null。

使用 WebClient - 不工作

private static void CallWebAPIUsingWebClient(string dataString, TokenResponse token, string url)
        {            
            using (WebClient client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                client.Headers[HttpRequestHeader.Accept] = "application/json";
                client.Headers.Add("Username", "WebClient");
                client.UploadString(url, "POST", dataString);
            }
        }

但是,如果我使用 WebRequest 或 HttpClient,我会在操作中获得完整的请求对象。

使用 WebRequest - 工作

private static void CallWebAPIUsingWebRequest(string dataString, TokenResponse token, string url)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Accept = "application/json";
            httpWebRequest.ContentType = "application/json";

            httpWebRequest.Method = "POST";
            httpWebRequest.Headers.Add("Username", "WebRequest");

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(dataString);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                string result = streamReader.ReadToEnd();
            }
        }

使用 HttpClient - 工作

private async static Task CallWebAPIUsingHttpClient(string dataString, TokenResponse token, string url)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("Username", "HttpClient");
                await client.PostAsync(url, new StringContent(dataString, System.Text.Encoding.UTF8, "application/json"));
            }
        }

奇怪的是,即使WebClient 一直工作到昨天。 请让我知道为什么,特别是 WebClient 不起作用。 提前致谢。

【问题讨论】:

    标签: c# asp.net json asp.net-web-api webclient


    【解决方案1】:

    检查您是否在以下方法中使用 HTTPPUT,因为您的原始 api 控制器操作方法接受 HTTPPOST

    private static void CallWebAPIUsingWebClient(string dataString, TokenResponse token, string middlewareUrl)
            {            
                using (WebClient client = new WebClient())
                {
                    client.Headers[HttpRequestHeader.ContentType] = "application/json";
                    client.Headers[HttpRequestHeader.Accept] = "application/json";
                    client.Headers[HttpRequestHeader.Authorization] = token.TokenType.ToLowerInvariant() + " " + token.AccessToken;
                    client.Headers.Add("Username", "WebClient");
                    client.UploadString(middlewareUrl, "PUT", dataString);
                }
            }
    

    【讨论】:

    • 是的,这是我发布的问题中的错字,我更新了我的问题。
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2015-08-24
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多