I am trying post data with HttpClient. My code is like this:
public static string[] SendData(string adress, Dictionary<string, string> data)
{
Task<string> komunikace = PostResponseString(adress, new FormUrlEncodedContent(data));
var vysledek = komunikace.Result;
return vysledek.ToString().Split('\n');
}
static async Task<string> PostResponseString(string uri, FormUrlEncodedContent data)
{
var response = await httpClient.PostAsync(uri, data);
var contents = await response.Content.ReadAsStringAsync();
return contents;
}
I want to send data.Value in windows-1250 encoding, cause site is written in this encoding.
Example: data.Key = apolozka
data.Value = příbalový leták 822
result: apoznamka=p%F8%EDbalov%FD+let%E1k+822
Don't now, how to do this. Sorry for my poor english.
EDIT:
This is my solution based on: HttpClient FormUrlEncodedContent Encoding Thank you Christoph Lütjen for your comment.
static async Task<string> PostResponseString(string uri, string postData)
{
string element = "";
string elementHttp;
int poziceAnd;
for (int i = 0; i < postData.Length; i++)
{
if (postData[i] == '=')
{
if (postData.Substring(i + 1).Contains('&'))
{
poziceAnd = postData.IndexOf('&', i);
element = postData.Substring(i + 1, poziceAnd - i - 1);
}
else
{
element = postData.Substring(i + 1);
}
elementHttp = HttpUtility.UrlEncode(element, Encoding.GetEncoding(myEncoding));
postData = postData.Replace(element, elementHttp);
}
}
byte[] bytes = System.Text.Encoding.GetEncoding(myEncoding).GetBytes(postData);
ByteArrayContent content = new ByteArrayContent(bytes);
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(uri, content);
byte[] bytesResp = await response.Content.ReadAsByteArrayAsync();
string odpoved = Encoding.GetEncoding(myEncoding).GetString(bytesResp, 0, bytesResp.Length);
return odpoved;
}