C# WebRequest - WebResponse

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by WAYS, 13 Mar 2010.

  1. WAYS

    WAYS New Member

    Joined:
    12 Nov 2008
    Messages:
    36
    Likes Received:
    0
    Reputations:
    0
    Всем привет, столкнулся с проблемой чтения ответа после пост запроса.
    Проблема в том что я делаю запрос, после чего я должен получить куки #1, потом, оно меня кидает на иную страницу и я получаю куки #2.

    Code:
                  string uri = "http://site.com/login.php";
                  string parameters = "username=" + tb_login.Text + "&password=" + tb_pass.Text;
                  WebRequest webRequest = WebRequest.Create(@uri);
                  Stream os = null;
                  webRequest.Method = "POST";
                  webRequest.Timeout = 15000;
                  webRequest.ContentType = "application/x-www-form-urlencoded";
                  webRequest.Headers.Add("Accept-Language", "ru-RU,ru;q=0.9,en;q=0.8");
                  webRequest.Headers.Add("Accept-Charset", "iso-8859-1, utf-8, utf-16, *;q=0.1");
                  webRequest.Headers.Add("Accept-Encoding", "deflate, gzip, x-gzip, identity, *;q=0");
                byte[] bytes = Encoding.UTF8.GetBytes(parameters);
                try
                { 
                    webRequest.ContentLength = bytes.Length;
                    os = webRequest.GetRequestStream();
                    os.Write(bytes, 0, bytes.Length); 
                }
                catch
                {
                }
                finally
                {
                    if (os != null)
                    {
                        os.Close();
                    }
                }
    
    WebResponse webResponse = webRequest.GetResponse();
                    MessageBox.Show(webResponse.Headers.ToString());
                    Меня в момент авторизации перекидывает на другую страницу, и я не получаю заголовки с 1й.
    
    
    
    Как получить куки 1?, я в чарлесе их вижу.
     
  2. Kaimi

    Kaimi Well-Known Member

    Joined:
    23 Aug 2007
    Messages:
    1,732
    Likes Received:
    811
    Reputations:
    231
    Мб стоит в начале написать
    webRequest.AllowAutoRedirect = false;
    и самостоятельно обрабатывать редирект
     
    _________________________
    1 person likes this.
  3. W!z@rD

    W!z@rD Борец за русский язык

    Joined:
    12 Feb 2006
    Messages:
    973
    Likes Received:
    290
    Reputations:
    43
    https://forum.antichat.ru/thread179068.html
     
  4. WAYS

    WAYS New Member

    Joined:
    12 Nov 2008
    Messages:
    36
    Likes Received:
    0
    Reputations:
    0
    Сделал
    Code:
            public string aut()
            {
                var url = "http://site.com/login.php";
                var postData = "username=" + tb_login.Text + "&password=" + tb_pass.Text;
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                string Cookies = null;
                httpWebRequest.AllowAutoRedirect = false;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                var buffer = Encoding.ASCII.GetBytes(postData);
                httpWebRequest.ContentLength = buffer.Length;
                try
                {
                    using (var writer = httpWebRequest.GetRequestStream())
                    {
                        writer.Write(buffer, 0, buffer.Length);
                    }
                    using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                    {
                        Settings1.Default.global_cookie = string.IsNullOrEmpty(httpWebResponse.Headers["Set-Cookie"]) ? "" : httpWebResponse.Headers["Set-Cookie"];
                        Settings1.Default.Save();
                        url = httpWebResponse.Headers.Get("Location") ?? "";
                    }
                    httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                    httpWebRequest.Method = "GET";
                    if (!string.IsNullOrEmpty(Cookies))
                    {
                        httpWebRequest.Headers.Add(HttpRequestHeader.Cookie, Cookies);
                    }
                    using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                    {
                        Settings1.Default.cookie = string.IsNullOrEmpty(httpWebResponse.Headers["Set-Cookie"]) ? "" : httpWebResponse.Headers["Set-Cookie"];
                        return httpWebResponse.ResponseUri.ToString();
                    }
                }
                catch (Exception er)
                {
                    this.Invoke(new UpdateResultCallback(this.UpdateResult), new object[] { er.Message });
                    return null;
                }
             }
    
     
    #4 WAYS, 14 Mar 2010
    Last edited: 14 Mar 2010