HttpWebRequest - не может отправить тело содержимого с этим типом глагола - PullRequest
0 голосов
/ 30 января 2020

У меня есть приложение, написанное на C#, которое использует HttpWebRequest для отправки запроса с телом. Чтобы добавить текст в запрос, я использую этот код:

WebRequest webRequest = WebRequest.Create(responseUri);

                ((HttpWebRequest)webRequest).Referer = responseUri;
                ((HttpWebRequest)webRequest).Host = "sts.mycompany.com";
                ((HttpWebRequest)webRequest).KeepAlive = true;
                ((HttpWebRequest)webRequest).AllowAutoRedirect = true;

                ((HttpWebRequest)webRequest).UseDefaultCredentials = true;

                ((HttpWebRequest)webRequest).UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                ((HttpWebRequest)webRequest).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

                ((HttpWebRequest)webRequest).Headers.Add("Cache-Control", "max-age=0");

                ((HttpWebRequest)webRequest).Headers.Add("Accept-Encoding", "gzip, deflate, br, peerdist");
                ((HttpWebRequest)webRequest).Headers.Add("Accept-Language", "en-US,en;q=0.5");
                //((HttpWebRequest)webRequest).Headers.Add("Accept-Charset", "ISO-8859-2,utf-8;q=0.7,*;q=0.3");
                ((HttpWebRequest)webRequest).Headers.Add("Upgrade-Insecure-Requests", @"1");
                ((HttpWebRequest)webRequest).Headers.Add("DNT", @"1");

                ((HttpWebRequest)webRequest).CookieContainer = CookieContainer;

                foreach (var cookie in CookieContainer.GetCookies(new Uri(responseUri)))
                {
                    Console.WriteLine(cookie.ToString()); 
                }

                ((HttpWebRequest)webRequest).Credentials = GetNetworkCredentials();
                ((HttpWebRequest)webRequest).AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                webRequest.Method = requestType.ToString();

                string msg = String.Format("UserName={0}&Password={1}&AuthMethod=FormsAuthentication", System.Net.WebUtility.UrlEncode(username), System.Net.WebUtility.UrlEncode(password));

                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();
                var originalString = response.ResponseUri.OriginalString;
                response.Close();

Ошибка в этой строке, когда я хочу добавить текст в тело:

byte [] msgb = System.Text. Encoding.UTF8.GetBytes (MSG); reqStream.Write (msgb, 0, msgb.Length);

ошибка: Cannot send a content-body with this verb-type и любая дополнительная информация.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...