0

我在使用 Content-Type = application/x-www-form-urlencoded 和 Https URL 调用 POST API 时需要帮助,我使用了下面的代码,但它一直返回 400 bad request 。

如何在 HttpClient 上以 x-www-form-urlencoded 格式发送数据?我下面的代码有什么问题。

               var request = (HttpWebRequest)WebRequest.Create(Config.AuthenticateAPIUrl);

                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                string responseText = "";



                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {

                    string data= "client_id="+Config.client_id+ "&client_secret=D]6uZ!2HPDBcouI:$jvbWtVw(g~5`&";
                   string strCallString = System.Web.HttpUtility.UrlEncode(data);
                    streamWriter.Write(strCallString);
                }

                var response = (HttpWebResponse)request.GetResponse();
                var encoding = ASCIIEncoding.ASCII;

                using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
                {
                    responseText = reader.ReadToEnd();
                    dynamic data = JsonConvert.DeserializeObject(responseText);

                    var access_token = data.access_token.value;

                    reader.Close();
                    response.Close();

                }
4

0 回答 0