I have a page on a site designed for adding a certain entity. What I'm trying to do is to add this entity using C# HttpClient. My sequence of steps looks like this:
First I'm authenticate using the client:
public static async Task<CookieCollection> WebPortalLogin(string baseURI, string phoneNo, string pin)
{
var cookies = new CookieContainer();
var handler = new HttpClientHandler()
{
CookieContainer = cookies
};
var client = new HttpClient(handler);
var content = new FormUrlEncodedContent(new[]{
new KeyValuePair<string,string>("page","login"),
new KeyValuePair<string,string>("noStaticBox",""),
new KeyValuePair<string,string>("username",phoneNo),
new KeyValuePair<string,string>("password",pin),
new KeyValuePair<string,string>("login","Увійти"),
new KeyValuePair<string,string>("_reqNo","0"),
});
var response = await client.PostAsync(baseURI, content);
response.EnsureSuccessStatusCode();
var stringResponse = response.Content.ReadAsStringAsync().Result;
var cookieJar = cookies.GetCookies(new Uri(baseURI));
return cookieJar;
}
Then I send POST request to edit page with all data I want to save:
public static async Task<HttpResponseMessage> AddCar(string baseURI, string phoneNo, CookieCollection cookieJar, string carNo, string owner)
{
var cookieContainer = new CookieContainer();
var client = new HttpClient(new HttpClientHandler() { CookieContainer = cookieContainer });
var content = new FormUrlEncodedContent(new[]{
new KeyValuePair<string,string>("carNo",carNo),
new KeyValuePair<string,string>("userName",owner),
new KeyValuePair<string,string>("page","carNumbers"),
new KeyValuePair<string,string>("submit","Додати"),
new KeyValuePair<string,string>("operation","addCar"),
new KeyValuePair<string,string>("_reqNo","0")
});
cookieContainer.Add(new Uri(baseURI), cookieJar);
var response = await client.PostAsync(baseURI, content);
var stringResponse = response.Content.ReadAsStringAsync().Result;
return response;
}
However, this POST request does nothing, and in response I have this very edit page, although when I add this entity in normal way (via web site), I get empty response and entity is successfully saved. Already checked cookies - they're all right. The only thing I can think of is request headers, but successful POST has only regular ones, like Accept, Accept-Encoding etc. What are my possible mistakes and how can I get it posted? Note: all connections use HTTPS.