【问题标题】:C# Get answer from HTTP POST RequestC# 从 HTTP POST 请求中获取答案
【发布时间】:2020-06-01 11:16:55
【问题描述】:

我正在进行 mail.ru 自动注册。为此,我在 C# 中发送此 POST 请求:

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace MailCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            ///creating a client
            HttpClient webclient = new HttpClient();

            ///sending a POST request
            var response = webclient.PostAsync("https://account.mail.ru/api/v1/user/signup", new FormUrlEncodedContent(new Dictionary<string, string>{ 
                {"extented", "true"},
                { "more_password_strength", "1" },
                { "context", "signup" },
                { "from", "main" },
                { "sent_me_ads", "true" },
                { "name", Convert.ToString(new Dictionary<string, string> { { "first", "John" }, {"last", "Genry"} }) },
                { "birthday", Convert.ToString(new Dictionary<string, int> { { "day", 4 }, {"month", 5}, {"year", 1995} }) },
                { "login", "pointlesst23" },
                { "domain", "mail.ru" },
                { "password", "Tim147890" },
            }));

            ///getting a Result
            var responseString = response.Result;
            Console.WriteLine(responseString);


        }
    }
}

问题是,我得到了这个答案:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Server: nginx
  Date: Mon, 01 Jun 2020 11:12:11 GMT
  Transfer-Encoding: chunked
  Connection: close
  X-XSS-Protection: 1; mode=block; report=https://cspreport.mail.ru/xxssprotection
  X-Host: fau37.m.smailru.net
  X-Host: fau37.m.smailru.net
  X-Content-Type-Options: nosniff
  X-Content-Type-Options: nosniff
  X-ComScore: pageview_candidate
  Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
  Strict-Transport-Security: max-age=15768000; includeSubDomains; preload
  X-Frame-Options: DENY
  Vary: Referer
  Content-Type: application/json; charset=utf-8
  Expires: Thu, 01 Jan 1970 00:00:01 GMT
}

但是,我需要另一个站点答案(我已经发送了这个 POST 请求,但是在 python 中)。因为其中有一个验证码 ID {"body":"mOlWAphGRBZWAIv7"}Full 需要的答案是:

{"body":"mOlWAphGRBZWAIv7","email":null,"status":200,"htmlencoded":true}

有人能解决这个问题吗?)

【问题讨论】:

  • 使用嗅探器并使用 python 将第一个请求中的标头与 c# 中的标头进行比较。 c# 中的默认标头不同。使 c# 中的标头看起来像工作 python。 c# 响应说拒绝,这意味着如果失败。看起来 SSL/TLS 验证失败了。

标签: c# .net post httpclient


【解决方案1】:

使用此代码

 var responseString = response.Result.Content.ReadAsStringAsync().Result;

作为

var responseString = response.Result;

但我建议使用等待/异步模式

 var responseString =await response; 
 var bodyResponseString =await responseString.Content.ReadAsStringAsync() ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 2016-05-03
    • 1970-01-01
    • 2017-02-11
    • 2017-10-03
    • 1970-01-01
    相关资源
    最近更新 更多