Posting data using httpwebrequest.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shankararaman.s@gmail.com

    #1

    Posting data using httpwebrequest.


    Hi,

    I am trying to develop an interface which will fetch all my Yahoo
    mails. I am not able to sign in to yahoo by posting the form with my
    username & password. Please find my code below and correct me where am
    going wrong.

    string result;
    System.Net.Http WebRequest request,request 1;
    System.Net.Http WebResponse response,respon se1;
    StreamReader sr;StreamWriter sw;
    request1 = (System.Net.Htt pWebRequest)
    System.Net.WebR equest.Create(" http://groups.yahoo.co m/");
    response1 = (System.Net.Htt pWebResponse) request1.GetRes ponse();
    string postData = "login=shansula k_2001&passwd=s hansulak";
    request =
    (HttpWebRequest )System.Net.Htt pWebRequest.Cre ate("http://login.yahoo.com/config/login?.intl=us& .src=ygrp&.done =http://groups.yahoo.co m");

    request.Method = "POST";
    request.AllowAu toRedirect = true;
    request.Content Type = "applicatio n/x-www-form-urlencoded";
    Encoding utf8 = new UTF8Encoding();
    byte[] content = utf8.GetBytes(p ostData);
    using (Stream requestStream = request.GetRequ estStream())
    {
    requestStream.W rite(content, 0, content.Length) ;

    }
    response = (HttpWebRespons e)request.GetRe sponse();
    sr = new StreamReader(re sponse.GetRespo nseStream());
    result = sr.ReadToEnd();
    sr.Close();
    Response.Write( result);

    Thanks in advance for your help.

    Regards,
    Shankara.

  • Joerg Jooss

    #2
    Re: Posting data using httpwebrequest.

    Thus wrote shankararaman.s @gmail.com,
    [color=blue]
    > Hi,
    >
    > I am trying to develop an interface which will fetch all my Yahoo
    > mails. I am not able to sign in to yahoo by posting the form with my
    > username & password. Please find my code below and correct me where am
    > going wrong.
    >
    > string result;
    >
    > System.Net.Http WebRequest request,request 1;
    >
    > System.Net.Http WebResponse response,respon se1;
    >
    > StreamReader sr;StreamWriter sw;
    >
    > request1 = (System.Net.Htt pWebRequest)
    >
    > System.Net.WebR equest.Create(" http://groups.yahoo.co m/");
    >
    > response1 = (System.Net.Htt pWebResponse) request1.GetRes ponse();
    >
    > string postData = "login=shansula k_2001&passwd=s hansulak";
    >
    > request =
    >
    > (HttpWebRequest )System.Net.Htt pWebRequest.Cre ate("http://login.yahoo.c
    > om/config/login?.intl=us& .src=ygrp&.done =http://groups.yahoo.co m");
    >
    > request.Method = "POST";
    > request.AllowAu toRedirect = true;
    > request.Content Type = "applicatio n/x-www-form-urlencoded";
    > Encoding utf8 = new UTF8Encoding();
    > byte[] content = utf8.GetBytes(p ostData);
    > using (Stream requestStream = request.GetRequ estStream())
    > {
    > requestStream.W rite(content, 0, content.Length) ;
    > }
    > response = (HttpWebRespons e)request.GetRe sponse();
    > sr = new StreamReader(re sponse.GetRespo nseStream());
    > result = sr.ReadToEnd();
    > sr.Close();
    > Response.Write( result);
    > Thanks in advance for your help.[/color]

    I would assume Yahoo sends a session cookie after successfully logging on.
    You need to use a System.Net.Cook ieContainer instance to keep track of cookies
    in your session.


    Cheers,
    --
    Joerg Jooss
    news-reply@joergjoos s.de


    Comment

    • David

      #3
      Re: Posting data using httpwebrequest.

      You might want to change your account password now that you have just told
      everyone what it is.

      Best regards,
      Dave Colliver.

      ~~
      http://www.FOCUSPortals.com - Local franchises available


      <shankararaman. s@gmail.com> wrote in message
      news:1140156666 .827537.305900@ g47g2000cwa.goo glegroups.com.. .[color=blue]
      >
      > Hi,
      >
      > I am trying to develop an interface which will fetch all my Yahoo
      > mails. I am not able to sign in to yahoo by posting the form with my
      > username & password. Please find my code below and correct me where am
      > going wrong.
      >
      > string result;
      > System.Net.Http WebRequest request,request 1;
      > System.Net.Http WebResponse response,respon se1;
      > StreamReader sr;StreamWriter sw;
      > request1 = (System.Net.Htt pWebRequest)
      > System.Net.WebR equest.Create(" http://groups.yahoo.co m/");
      > response1 = (System.Net.Htt pWebResponse) request1.GetRes ponse();
      > string postData = "login=shansula k_2001&passwd=s hansulak";
      > request =
      > (HttpWebRequest )System.Net.Htt pWebRequest.Cre ate("http://login.yahoo.com/config/login?.intl=us& .src=ygrp&.done =http://groups.yahoo.co m");
      >
      > request.Method = "POST";
      > request.AllowAu toRedirect = true;
      > request.Content Type = "applicatio n/x-www-form-urlencoded";
      > Encoding utf8 = new UTF8Encoding();
      > byte[] content = utf8.GetBytes(p ostData);
      > using (Stream requestStream = request.GetRequ estStream())
      > {
      > requestStream.W rite(content, 0, content.Length) ;
      >
      > }
      > response = (HttpWebRespons e)request.GetRe sponse();
      > sr = new StreamReader(re sponse.GetRespo nseStream());
      > result = sr.ReadToEnd();
      > sr.Close();
      > Response.Write( result);
      >
      > Thanks in advance for your help.
      >
      > Regards,
      > Shankara.
      >[/color]


      Comment

      • tdavisjr

        #4
        Re: Posting data using httpwebrequest.


        David wrote:[color=blue]
        > You might want to change your account password now that you have just told
        > everyone what it is.
        >[/color]

        WOW. This was not a wise thing to do.


        [color=blue]
        > Best regards,
        > Dave Colliver.
        > http://www.AshfieldFOCUS.com
        > ~~
        > http://www.FOCUSPortals.com - Local franchises available
        >
        >
        > <shankararaman. s@gmail.com> wrote in message
        > news:1140156666 .827537.305900@ g47g2000cwa.goo glegroups.com.. .[color=green]
        > >
        > > Hi,
        > >
        > > I am trying to develop an interface which will fetch all my Yahoo
        > > mails. I am not able to sign in to yahoo by posting the form with my
        > > username & password. Please find my code below and correct me where am
        > > going wrong.
        > >
        > > string result;
        > > System.Net.Http WebRequest request,request 1;
        > > System.Net.Http WebResponse response,respon se1;
        > > StreamReader sr;StreamWriter sw;
        > > request1 = (System.Net.Htt pWebRequest)
        > > System.Net.WebR equest.Create(" http://groups.yahoo.co m/");
        > > response1 = (System.Net.Htt pWebResponse) request1.GetRes ponse();
        > > string postData = "login=shansula k_2001&passwd=s hansulak";
        > > request =
        > > (HttpWebRequest )System.Net.Htt pWebRequest.Cre ate("http://login.yahoo.com/config/login?.intl=us& .src=ygrp&.done =http://groups.yahoo.co m");
        > >
        > > request.Method = "POST";
        > > request.AllowAu toRedirect = true;
        > > request.Content Type = "applicatio n/x-www-form-urlencoded";
        > > Encoding utf8 = new UTF8Encoding();
        > > byte[] content = utf8.GetBytes(p ostData);
        > > using (Stream requestStream = request.GetRequ estStream())
        > > {
        > > requestStream.W rite(content, 0, content.Length) ;
        > >
        > > }
        > > response = (HttpWebRespons e)request.GetRe sponse();
        > > sr = new StreamReader(re sponse.GetRespo nseStream());
        > > result = sr.ReadToEnd();
        > > sr.Close();
        > > Response.Write( result);
        > >
        > > Thanks in advance for your help.
        > >
        > > Regards,
        > > Shankara.
        > >[/color][/color]

        Comment

        • Shankar

          #5
          Re: Posting data using httpwebrequest.

          I have included code for saving cookies. But still not working. Please
          Help.

          string result;
          System.Net.Http WebRequest request,request 1;
          System.Net.Http WebResponse response,respon se1;
          StreamReader sr;StreamWriter sw;
          request1 = (System.Net.Htt pWebRequest)
          System.Net.WebR equest.Create(" http://groups.yahoo.co m/");
          response1 = (System.Net.Htt pWebResponse) request1.GetRes ponse();

          string postData = "login=id_for_t esting&passwd=t estingGroup";
          request =
          (HttpWebRequest )System.Net.Htt pWebRequest.Cre ate("http://login.yahoo.com/config/login?.intl=us& .src=ygrp&.done =http://groups.yahoo.co m");

          request.Method = "POST";
          request.AllowAu toRedirect = true;
          request.Content Type = "applicatio n/x-www-form-urlencoded";
          request.Timeout = 100000;
          request.CookieC ontainer = new CookieContainer ();
          request.CookieC ontainer.Add(re sponse1.Cookies );
          Encoding utf8 = new UTF8Encoding();
          byte[] content = utf8.GetBytes(p ostData);
          request.Content Length = content.Length;
          using (Stream requestStream = request.GetRequ estStream())
          {
          requestStream.W rite(content, 0, content.Length) ;
          }
          response = (HttpWebRespons e)request.GetRe sponse();
          sr = new StreamReader(re sponse.GetRespo nseStream());
          result = sr.ReadToEnd();
          sr.Close();
          Response.Write( "Server: "+response.Serv er +" Code:
          "+response.Stat usCode + " Desc: " + response.Status Description);
          Response.Write( result);

          Thanks & Regards,
          Shankara.

          Thanks a lot for pointing out that I have leaked out my mail passwd
          Dave :-) I have changed it

          Comment

          • Joerg Jooss

            #6
            Re: Posting data using httpwebrequest.

            Thus wrote Shankar,
            [color=blue]
            > I have included code for saving cookies. But still not working. Please
            > Help.
            >
            > string result;
            > System.Net.Http WebRequest request,request 1;
            > System.Net.Http WebResponse response,respon se1;
            > StreamReader sr;StreamWriter sw;
            > request1 = (System.Net.Htt pWebRequest)
            > System.Net.WebR equest.Create(" http://groups.yahoo.co m/");
            > response1 = (System.Net.Htt pWebResponse) request1.GetRes ponse();
            > string postData = "login=id_for_t esting&passwd=t estingGroup";
            >
            > request =
            >
            > (HttpWebRequest )System.Net.Htt pWebRequest.Cre ate("http://login.yahoo.c
            > om/config/login?.intl=us& .src=ygrp&.done =http://groups.yahoo.co m");
            >
            > request.Method = "POST";
            > request.AllowAu toRedirect = true;
            > request.Content Type = "applicatio n/x-www-form-urlencoded";
            > request.Timeout = 100000;
            > request.CookieC ontainer = new CookieContainer ();
            > request.CookieC ontainer.Add(re sponse1.Cookies );[/color]

            CookieContainer tracks cookies automatically. You don't need to copy them
            yourself (and at this point, there will be no session cookie, yet).
            [color=blue]
            > Encoding utf8 = new UTF8Encoding();
            > byte[] content = utf8.GetBytes(p ostData);
            > request.Content Length = content.Length;
            > using (Stream requestStream = request.GetRequ estStream())
            > {
            > requestStream.W rite(content, 0, content.Length) ;
            > }
            > response = (HttpWebRespons e)request.GetRe sponse();
            > sr = new StreamReader(re sponse.GetRespo nseStream());
            > result = sr.ReadToEnd();
            > sr.Close();
            > Response.Write( "Server: "+response.Serv er +" Code:
            > "+response.Stat usCode + " Desc: " + response.Status Description);
            > Response.Write( result);[/color]

            So what happens at this point? Do you get redirected back to the login page?
            Do you get an error page?

            You should also verify that Yahoo uses cookies. I've blindly assumed that,
            because it's the most common approach for session tracking, but if they're
            using URL rewriting, all of the above won't really help.

            Cheers,
            --
            Joerg Jooss
            news-reply@joergjoos s.de


            Comment

            • Shankar

              #7
              Re: Posting data using httpwebrequest.

              Hi Jeorg,

              I get a page where it says " The page cannot be found. HTTP 404 - File
              not found". I get the Statuscode as "OK" and StatusDescripti on also
              "OK". I am not getting emptystring for response.Server . I think Yahoo
              does use cookies. Because, when I donot use the cookie container, it
              says, "cookies rejected" error.

              Thanks & Regrds,
              Shankara.

              Comment

              • Shankar

                #8
                Re: Posting data using httpwebrequest.

                Sorry. I am getting an emptystring as response for response.Server

                Thanks & Regards,
                Shankara.

                Comment

                • Joerg Jooss

                  #9
                  Re: Posting data using httpwebrequest.

                  Thus wrote Shankar,
                  [color=blue]
                  > Hi Jeorg,
                  >
                  > I get a page where it says " The page cannot be found. HTTP 404 - File
                  > not found". I get the Statuscode as "OK" and StatusDescripti on also
                  > "OK". I am not getting emptystring for response.Server . I think Yahoo
                  > does use cookies. Because, when I donot use the cookie container, it
                  > says, "cookies rejected" error.[/color]

                  That means you were either redirected to a non-existing page or you requested
                  a non-existing page.

                  It's not uncommon that there's no Server header. Many organizations don't
                  want to reveal what kind of server they are running, due to security concerns.

                  Cheers,
                  --
                  Joerg Jooss
                  news-reply@joergjoos s.de


                  Comment

                  Working...