C#从HTTP POST请求中获取答案

问题描述 投票:0回答:1

我正在进行mail.ru自动注册。为此,我在C#中发送了这个POST-Request。

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-request,但在python中)。因为其中有一个验证码ID。{"body":"mOlWAphGRBZWAIv7"}完整需要的答案是。

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

谁能解决这个问题?)

c# .net post httpclient
1个回答
0
投票

使用这段代码

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

作为

var responseString = response.Result;

但我建议使用 awaitasync 模式

 var responseString =await response; 
 var bodyResponseString =await responseString.Content.ReadAsStringAsync() ;
© www.soinside.com 2019 - 2024. All rights reserved.