Last active
May 11, 2016 18:43
-
-
Save stefanhendriks/8ed40d617ce3a339af3e77e5dcd4561c to your computer and use it in GitHub Desktop.
Helper class to construct a HttpRequest message for easily POSTing data (Asp.net Core)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PostRequestHelper | |
{ | |
public static HttpRequestMessage Create(String path, Dictionary<string, string> formPostBodyData) | |
{ | |
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, path) | |
{ | |
Content = new FormUrlEncodedContent(ToFormPostData(formPostBodyData)) | |
}; | |
return httpRequestMessage; | |
} | |
public static List<KeyValuePair<string, string>> ToFormPostData(Dictionary<string, string> formPostBodyData) | |
{ | |
List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>(); | |
formPostBodyData.Keys.ToList().ForEach(key => | |
{ | |
result.Add(new KeyValuePair<string, string>(key, formPostBodyData[key])); | |
}); | |
return result; | |
} | |
public static HttpRequestMessage CreateWithCookiesFromResponse(string path, Dictionary<string, string> formPostBodyData, | |
HttpResponseMessage response) | |
{ | |
var httpRequestMessage = Create(path, formPostBodyData); | |
return CookiesHelper.CopyCookiesFromResponse(httpRequestMessage, response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CookiesHelper mentioned at #25 is https://gist.github.com/stefanhendriks/791391f10a40bde160568cf5ca79cc53