简体   繁体   English

如何真正使用HttpRequestMessage,避免HttpClient PostAsync 500错误?

[英]How to use HttpRequestMessage truely and avoid Error 500 in HttpClient PostAsync?

It is supposed to fill "textbox1" with string value and get another string in textbox3.它应该用字符串值填充“textbox1”并在 textbox3 中获取另一个字符串。 For this job, I inspected ASP form Elements in the website and checked in "Network" section the "POST" activities when I fill the form and submit it.Therefore I wrote this code for sending the post request and getting the response.对于这项工作,我检查了网站中的 ASP 表单元素,并在填写表单并提交时在“网络”部分检查了“POST”活动。因此,我编写了这段代码来发送 post 请求并获得响应。 But I get the error which tells me "Make sure request headers are used with "HttpRequestMessage" and "HttpResponseMessage". Before I adding the headers section I got Error 500 in console. please tell me the correct coding.但我得到的错误告诉我“确保请求标头与“HttpRequestMessage”和“HttpResponseMessage”一起使用。在我添加标头部分之前,我在控制台中收到错误 500。请告诉我正确的编码。

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;



namespace WebRequest
{
    class myRequest3
    {
      
        public async static void submitpost()
        {
            using (var client = new HttpClient())
            {
                var values = new Dictionary<string, string>
        {
           {"ctl00$ScriptManager1","ctl00$content$ctl00|ctl00$content$Button4" },
           {"__EVENTTARGET","" },
           {"__EVENTARGUMENT","" },
           {"__VIEWSTATE", "/wEPDwUKLTM1MTY1NjE1Nw9kFgJmD2QWAgIJD2QWAgIDD2QWAgIDD2QWAgIBD2QWAmYPZBYCAgEPDxYCHgZfVGl0bGUFGtiz2LHbjNin2YQg2YHYudin2YQg2LPYp9iyZGRkP5ZIUYR1R/p/JMF4Ez0q2psS7pQ=    " },
           {"__VIEWSTATEGENERATOR", "CA0B0334" },
           {"__EVENTVALIDATION", "/wEWBAL6k4rsBQLt4KPyCgL84ejwCALt4KvyCh3brVZZShAeom3oNb8A0uWDpqDK" },
            {"ctl00$content$TextBox1", "hfc0Ac0TIaFPrpef5IGZJQTrCrBdm4S+" },
            {"ctl00$content$TextBox3", "" },
            {"__ASYNCPOST", "true" },
            {"ctl00$content$Button4", "ساخت سریال فعال ساز" },
        };

                var content = new FormUrlEncodedContent(values);
                client.DefaultRequestHeaders.Add("Accept","*/*");
                client.DefaultRequestHeaders.Add("Accept-Encoding","gzip, deflate");
                client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");
                client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
                client.DefaultRequestHeaders.Add("Connection","keep-alive");
                client.DefaultRequestHeaders.Add("Content-Length", "641");
                client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
                client.DefaultRequestHeaders.Add("Host","address");
                client.DefaultRequestHeaders.Add("Origin","http://address");
                client.DefaultRequestHeaders.Add("Referer","http://address/");
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0");
                client.DefaultRequestHeaders.Add("X-MicrosoftAjax","Delta=true");


                var response = await client.PostAsync("http://address/default.aspx", content);

                var responseString = await response.Content.ReadAsStringAsync();
                
                Console.WriteLine(responseString);
                

            }

            
        }



    }
}

There are few things you need to notice: Default request headers - some of them are predefined in content or httpclient class, so you should never do that, eg the line that is breaking your code您需要注意几件事: 默认请求标头 - 其中一些是在内容或 httpclient class 中预定义的,因此您永远不应该这样做,例如破坏代码的行

client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

will always give you error, since that is being done when you're generating content.总会给你错误,因为这是在你生成内容时完成的。 So you can check it like this:所以你可以这样检查:

var content = new FormUrlEncodedContent(values);
var checkContentType = content.Headers.ContentType;// == "application/x-www-form-urlencoded; 

So, if we redactor your headers code a little bit with only necessary one's that's what we get:因此,如果我们只使用必要的代码对您的标头代码进行一些编辑,那就是我们得到的:

client.DefaultRequestHeaders.Add("Accept", "*/*"); //ok
//  client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate"); // unnecessary
//  client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5"); // unnecessary
//  client.DefaultRequestHeaders.Add("Cache-Control", "no-cache"); // unnecessary
//  client.DefaultRequestHeaders.Add("Connection", "keep-alive");
//  client.DefaultRequestHeaders.Add("Content-Length", "641"); // creating content handles that - unnecessary
          
//  client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); // already set when creating content
//  client.DefaultRequestHeaders.Add("Host", "address"); // unnecessary
//  client.DefaultRequestHeaders.Add("Origin", "http://address"); // unnecessary
//  client.DefaultRequestHeaders.Add("Referer", "http://address/"); //unnecessary
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0");
//  client.DefaultRequestHeaders.Add("X-MicrosoftAjax", "Delta=true"); // unnecessary

After I run the code now the response is 200 (ok), but server returns "0|error|500||"现在运行代码后,响应为 200(正常),但服务器返回“0|error|500||” so you will need to check server side params/form data that are required or missing.因此您需要检查服务器端参数/表单数据是否需要或缺失。

Error 500 was solved by revising dictionary values.错误 500 已通过修改字典值解决。 I have entered "__VIEWSTATE" value incorrectly, besides for the first error, some of the values were unnecessary.我错误地输入了“__VIEWSTATE”值,除了第一个错误外,有些值是不必要的。 by parsing the responseString I could reach to textbox3 value.通过解析 responseString 我可以达到 textbox3 值。 Here is the correct code:这是正确的代码:

                var client = new HttpClient();    
                var values = new Dictionary<string, string>{
                    {"__VIEWSTATE", "/wEPDwUKLTM1MTY1NjE1Nw9kFgJmD2QWAgIJD2QWAgIDD2QWAgIDD2QWAgIBD2QWAmYPZBYCAgEPDxYCHgZfVGl0bGUFGtiz2LHbjNin2YQg2YHYudin2YQg2LPYp9iyZGRkP5ZIUYR1R/p/JMF4Ez0q2psS7pQ=" },
                    {"__EVENTVALIDATION", "/wEWBAL6k4rsBQLt4KPyCgL84ejwCALt4KvyCh3brVZZShAeom3oNb8A0uWDpqDK" },
                    {"ctl00$content$TextBox1", "hfd0Ac0TIaFPrpef5IGZJQTrCrBdm4S+" },
                    {"ctl00$content$Button4", "ساخت سریال فعال ساز" },
                };
                var content = new FormUrlEncodedContent(values);
                client.DefaultRequestHeaders.Add("Accept", "*/*");                    
                HttpResponseMessage response = await client.PostAsync("http://address/default.aspx", content);
                Thread.Sleep(50);
                string responseString = await response.Content.ReadAsStringAsync();
                Thread.Sleep(50);
                string textbox3Value= HTMLValue(responseString, "ctl00_content_TextBox3");
                Console.WriteLine("finished");    

Here is a custom code I wrote for extracting textBox3 value:这是我为提取 textBox3 值而编写的自定义代码:

  public static string HTMLValue(string responseResult, string xElement)
        {
            int x = 0;
            int y = 0;
            string value = "";
            x = responseResult.IndexOf(xElement);
            responseResult = responseResult.Substring(x + 1);
            x = responseResult.IndexOf(">");
            y = responseResult.IndexOf("<");
            if(y-x-1>0)value = responseResult.Substring(x + 1, y - x - 1);
            return (value);
        }

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

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