Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var jsonString = JsonConvert.SerializeObject(post_parameters);
- var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
- public class MyFormUrlEncodedContent : ByteArrayContent
- {
- public MyFormUrlEncodedContent(IEnumerable<KeyValuePair<string, string>> nameValueCollection)
- : base(MyFormUrlEncodedContent.GetContentByteArray(nameValueCollection))
- {
- base.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
- }
- private static byte[] GetContentByteArray(IEnumerable<KeyValuePair<string, string>> nameValueCollection)
- {
- if (nameValueCollection == null)
- {
- throw new ArgumentNullException("nameValueCollection");
- }
- StringBuilder stringBuilder = new StringBuilder();
- foreach (KeyValuePair<string, string> current in nameValueCollection)
- {
- if (stringBuilder.Length > 0)
- {
- stringBuilder.Append('&');
- }
- stringBuilder.Append(MyFormUrlEncodedContent.Encode(current.Key));
- stringBuilder.Append('=');
- stringBuilder.Append(MyFormUrlEncodedContent.Encode(current.Value));
- }
- return Encoding.Default.GetBytes(stringBuilder.ToString());
- }
- private static string Encode(string data)
- {
- if (string.IsNullOrEmpty(data))
- {
- return string.Empty;
- }
- return System.Net.WebUtility.UrlEncode(data).Replace("%20", "+");
- }
- }
- void sendDocument()
- {
- string url = "www.mysite.com/page.php";
- StringBuilder postData = new StringBuilder();
- postData.Append(String.Format("{0}={1}&", HttpUtility.HtmlEncode("prop"), HttpUtility.HtmlEncode("value")));
- postData.Append(String.Format("{0}={1}", HttpUtility.HtmlEncode("prop2"), HttpUtility.HtmlEncode("value2")));
- StringContent myStringContent = new StringContent(postData.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded");
- HttpClient client = new HttpClient();
- HttpResponseMessage message = client.PostAsync(url, myStringContent).GetAwaiter().GetResult();
- string responseContent = message.Content.ReadAsStringAsync().GetAwaiter().GetResult();
- }
Add Comment
Please, Sign In to add comment