0

我正在尝试使用 c# 发出 PostAsync 请求。以下是我的代码。

       static void Main(string[] args)
    {

        CookieContainer cookies = new CookieContainer();
        HttpClientHandler handler = new HttpClientHandler();
        handler.CookieContainer = cookies;
        using (var client = new HttpClient(handler))
        {
            using (HttpResponseMessage getResponse = client.GetAsync("http://google.com").Result)
            {
                CheckStatusCode(getResponse);
                String header = getResponse.Headers.ToString();
                var content = CreateCollection(getResponse.Content);
                var _stringHeaderContent = header + "\r" + content;

                HttpContent _content = new StringContent(_stringHeaderContent, Encoding.UTF8);
                Console.WriteLine(_content);
                using (HttpResponseMessage postResponse = client.PostAsync("http://google.com",_content).Result)
                {
                    Console.WriteLine(postResponse.Headers);
                    CheckStatusCode(postResponse);
                    Console.WriteLine(postResponse.Headers);
                }
            }

        }
    }

方法:

public static void CheckStatusCode(HttpResponseMessage response)
    {
        if (response.StatusCode != HttpStatusCode.OK)
            throw new Exception(String.Format(
           "Server error (HTTP {0}: {1}).",
           response.StatusCode,
           response.ReasonPhrase));
        else
            Console.WriteLine("200");
    }
    public static String CreateCollection(HttpContent content)
    {
        var myContent = content.ReadAsStringAsync().Result;
        HtmlNode.ElementsFlags.Remove("form");
        string html = myContent;
        var doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(html);
        var input = doc.DocumentNode.SelectSingleNode("//*[@name='__Token']");
        var token = input.Attributes["value"].Value;
        //add all necessary component to collection
        NameValueCollection collection = new NameValueCollection();
        collection.Add("__Token", token);
        collection.Add("returnURL", "");
        collection.Add("Email", "11111111@hotmail.com");
        collection.Add("Password", "1234");
        String queryString = GenerateQueryString(collection);
        return queryString;
    }
    public static string GenerateQueryString(NameValueCollection collection)
    {
        var array = (from key in collection.AllKeys
                     from value in collection.GetValues(key)
                     select string.Format("{0}={1}", WebUtility.UrlEncode(key), WebUtility.UrlEncode(value))).ToArray();
        return string.Join("&", array);
    }

我在使用此代码时遇到的问题是_content 中没有存储任何内容。我正在尝试使用标头和加入的部分内容发出 postAsync 请求。但是,当我发出 postAsync 请求时,_content 中没有存储任何内容。因此请求失败。

谁能向我解释如何使用 _stringHeaderContent 发出 postAsync 请求?

问候

4

1 回答 1

0

这是如何使用 PostAsync 的方式:

...
var httpClient = new HttpClient();

var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("a", "1"));
postData.Add(new KeyValuePair<string, string>("b", "2"));
var content = new FormUrlEncodedContent(postData);

var result = httpClient.PostAsync("http://127.0.0.1/a.php", content).Result;
...
于 2016-08-07T01:53:05.377 回答