繁体   English   中英

C# HttpWebRequest 发送 xhr 请求 - 400 错误请求

[英]C# HttpWebRequest send xhr request - 400 bad request

我必须从 C# 发送 ajax 请求。 在浏览器中,请求如下所示:

Request URL:https://sts-service.mycompany.com/UPNFromUserName
Request method:POST
Remote address:xxxx
Status code:
200
Version:HTTP/2.0
Referrer Policy:strict-origin-when-cross-origin

标题:

 Host: sts-service.mycompany.com
 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
 Accept: */*
 Accept-Language: en-US,en;q=0.5
 Accept-Encoding: gzip, deflate, br
 Referer: https://sts.mycompany.com/
 Content-type: application/x-www-form-urlencoded
 Content-Length: 17
 Origin: https://sts.mycompany.com
 DNT: 1
 Connection: keep-alive
 Pragma: no-cache
 Cache-Control: no-cache

参数:表单数据:

 MS.Aution

饼干:没有饼干

在 C# 中,我的要求是:

WebRequest webRequest = WebRequest.Create("https://sts-service.mycompany.com/UPNFromUserName");

((HttpWebRequest)webRequest).Referer = "https://sts.mycompany.com/";
((HttpWebRequest)webRequest).Host = "sts-service.mycompany.com";
((HttpWebRequest)webRequest).KeepAlive = true;
((HttpWebRequest)webRequest).AllowAutoRedirect = true;
((HttpWebRequest)webRequest).UseDefaultCredentials = true;
((HttpWebRequest)webRequest).UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0";
webRequest.ContentType = "application/x-www-form-urlencoded";
((HttpWebRequest)webRequest).Accept = "*/*";

((HttpWebRequest)webRequest).Headers.Add("Origin", "https://sts.mycompany.com");
((HttpWebRequest)webRequest).Headers.Add("Accept-Encoding", "gzip, deflate, br");
((HttpWebRequest)webRequest).Headers.Add("Accept-Language", "en-US,en;q=0.5");
((HttpWebRequest)webRequest).Headers.Add("Upgrade-Insecure-Requests", @"1");
((HttpWebRequest)webRequest).Headers.Add("DNT", @"1");
((HttpWebRequest)webRequest).AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
webRequest.Method = HttpRequestType.POST.ToString();
string msg = "MSCnE.Automation";

webRequest.ContentLength = msg.Length;
Stream reqStream = webRequest.GetRequestStream();
byte[] msgb = System.Text.Encoding.UTF8.GetBytes(msg);
reqStream.Write(msgb, 0, msgb.Length);
reqStream.Close();
var response = (HttpWebResponse)webRequest.GetResponse();

StreamReader sr = new StreamReader(response.GetResponseStream());
string Result = sr.ReadToEnd();
response.Close();

我得到错误:

远程服务器返回错误:(400) 错误请求。

在网络选项卡的浏览器中,请求如下所示: 在此处输入图片说明

类型是json但在 Headers 内容类型是application/x-www-form-urlencoded也许这就是原因?

沿着这些路线的东西(未测试):

    public async Task<string> SendPOST()
            {
                var dict = new Dictionary<string, string>();
                dict.Add("DNT", "1");
                dict.Add("someformdata","MSCnE.Automation");
                using (var formdata = new System.Net.Http.FormUrlEncodedContent(dict))
                {
                    //do not use using HttpClient() - https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient
                    using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())    
                    {
                        formdata.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
                        formdata.Headers.ContentType.CharSet = "UTF-8";
                        httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0");
                        using (var response = await httpClient.PostAsync("https://sts-service.mycompany.com/UPNFromUserName", formdata))
                        {
                            if (response.IsSuccessStatusCode)
                            {
                                var postresult = await response.Content.ReadAsStringAsync();
                                return postresult;
                            }
                            else
                            {
                                string errorresult = await response.Content.ReadAsStringAsync();
                                return errorresult;
                            }
                        }
                    }
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM