postAsync avec en-tête et le contenu de c#

J'ai besoin de postAsync avec en-tête et du contenu. Afin d'obtenir l'accès à un site web grâce à l'Application Console en C#. J'ai mes en-têtes comme un HttpHeader objet avec le nom de la variable de l'en-tête et le contenu de mon nommée newContent comme un objet de type string avec __Token, return, Email et Password. Maintenant ce que je veux faire est d'ajouter newContent à-tête, puis utiliser postAsync(url, header+content) pour faire ma requête POST.

public async static void DownloadPage(string url)
{
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
using (HttpClient client = new HttpClient(handler))
{
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
//statusCode
CheckStatusCode(response);
//header
HttpHeaders headers = response.Headers;
//content
HttpContent content = response.Content;
//getRequestVerificationToken&createCollection
string newcontent = CreateCollection(content);
using(HttpResponseMessage response2 = client.PostAsync(url,))
}
}
}
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);
}
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("return", "");
collection.Add("Email", "[email protected]");
collection.Add("Password", "1234");
var newCollection = GenerateQueryString(collection);
return newCollection;
}
  • que voulez-vous dire? Je ne sais pas comment le faire... @x...
InformationsquelleAutor Puzzle | 2016-08-05