I'm trying to log in to the site through a post request

string reqString = "http://ДОМЕН/panel/login&login=" + login.Text + "&pass=" + pass.Text; var client = WebRequest.Create(reqString); client.Headers.Set("Authorization", "Basic YWRtaW46MTIzNDU="); client.Headers["Authorization"] = "YWRtaW46MTIzNDU="; client.Headers.Add(String.Format("X-Requested-With:{0}", "XMLHttpRequest")); client.Method = "POST"; client.ContentType = "application/x-www-form-urlencodedn"; HttpWebResponse resp; using (resp = (HttpWebResponse)client.GetResponse()) { if (resp.GetResponseStream() != null) { var status = resp.StatusDescription; var reader = new StreamReader(resp.GetResponseStream()); var obj = reader.ReadToEnd(); } } 

Authorization passes through Ajax, post request, how to save the received cookies? And make up a condition like: IF authorization = TRUTH - then output that then ...?

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants dlarchikov , user194374, αλεχολυτ , vp_arth , Streletz 22 Feb '17 at 20:29

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Or how can I login through the application on my site? - Elizabeth

1 answer 1

 private async Task<string> Authorize(string userName, string passwd) { var uri = new Uri("http://ДОМЕН/panel/login"); // Тело запроса var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("login", userName), new KeyValuePair<string, string>("pass", password) }); var client = new HttpClient(); // Добавляешь нужные тебе заголовки, для примера взял Accept-type client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.PostAsync(uri.ToString(), formContent); var responseContent = await response.Content.ReadAsStringAsync(); dynamic jsonResult = JObject.Parse(responseContent); // Тут уже зависит от структуры ответа на запрос. // Для примера достаешь status из запроса. return jsonResult.status; } public async Task<ActionResult> LoginInExternal() { var response= await Authorize("login@login.ru", "password"); response == 1 ? Console.WriteLine("Авторизован") : Console.WriteLine("Не Авторизован"); } 

If the request does not go, you can test with the help of any utility like Postman. On Windows, it is installed via Chrome extensions, for Mac, as I recall, from the AppStore.

  • Check, accomplish your goal, thank you! - Elizabeth
  • @ Elizabeth You also specify in what format the answer comes to you. JSON or XML? - Maxim Shinkarev
  • My studio has replaced half of the code, but in the end it still does not want to work ( - Elizaveta
  • @ Elizabeth What does "half code replaced"? Error compiling or just failing the request? - Maxim Shinkarev
  • compilation error, httpclient and other classes ... rework methods - Elizabeth