Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Submit a web form: What I'm doing wrong? [modified]

Submit a web form: What I'm doing wrong? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
phphtmlhelpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    msn92
    wrote on last edited by
    #1

    Hi everybody. I want to submit a web form. Source of html page:

    <html>
    <body>

    <form action="i.php" method="post">
    Name: <input type="text" name="fname" value="MyName" />
    Age: <input type="text" name="age" value="17" />
    <input type="submit" />
    </form>

    </body>
    </html>

    If I submit the form in the page above, I would get:

    Welcome MyName!
    You are 17 years old.

    And this is my code:

    CInternetSession session(L"MyAgent");
    CHttpConnection* httpcon=session.GetHttpConnection(L"localhost");
    CHttpFile* httpfile=httpcon->OpenRequest(CHttpConnection::HTTP_VERB_POST,L"i.php");
    CString headers=L"Content-Type: application/x-www-form-urlencoded";
    CString data=L"fname=MyName&age=17";
    httpfile->SendRequest(headers,(LPVOID)(LPCTSTR)data,data.GetLength());
    CString t=L"";
    UINT len=100;
    char buf[100];
    while(len>0){
    len=httpfile->Read(buf,100);
    if(len>0)t.Append(CString(buf),len);
    }
    AfxMessageBox(t);
    httpfile->Close();
    httpcon->Close();
    session.Close();

    Unfortunately, form is not being submitted. What I'm doing wrong? I'm trying to do this thing 3rd time now, I don't want to give up this time :-D Any help would be greatly appreciated.

    modified on Thursday, August 27, 2009 6:28 PM

    Z S 2 Replies Last reply
    0
    • M msn92

      Hi everybody. I want to submit a web form. Source of html page:

      <html>
      <body>

      <form action="i.php" method="post">
      Name: <input type="text" name="fname" value="MyName" />
      Age: <input type="text" name="age" value="17" />
      <input type="submit" />
      </form>

      </body>
      </html>

      If I submit the form in the page above, I would get:

      Welcome MyName!
      You are 17 years old.

      And this is my code:

      CInternetSession session(L"MyAgent");
      CHttpConnection* httpcon=session.GetHttpConnection(L"localhost");
      CHttpFile* httpfile=httpcon->OpenRequest(CHttpConnection::HTTP_VERB_POST,L"i.php");
      CString headers=L"Content-Type: application/x-www-form-urlencoded";
      CString data=L"fname=MyName&age=17";
      httpfile->SendRequest(headers,(LPVOID)(LPCTSTR)data,data.GetLength());
      CString t=L"";
      UINT len=100;
      char buf[100];
      while(len>0){
      len=httpfile->Read(buf,100);
      if(len>0)t.Append(CString(buf),len);
      }
      AfxMessageBox(t);
      httpfile->Close();
      httpcon->Close();
      session.Close();

      Unfortunately, form is not being submitted. What I'm doing wrong? I'm trying to do this thing 3rd time now, I don't want to give up this time :-D Any help would be greatly appreciated.

      modified on Thursday, August 27, 2009 6:28 PM

      Z Offline
      Z Offline
      zhu_lin
      wrote on last edited by
      #2

      check your localhost port in the class CInternetSession.

      it's my pleasure to make friend with you.

      1 Reply Last reply
      0
      • M msn92

        Hi everybody. I want to submit a web form. Source of html page:

        <html>
        <body>

        <form action="i.php" method="post">
        Name: <input type="text" name="fname" value="MyName" />
        Age: <input type="text" name="age" value="17" />
        <input type="submit" />
        </form>

        </body>
        </html>

        If I submit the form in the page above, I would get:

        Welcome MyName!
        You are 17 years old.

        And this is my code:

        CInternetSession session(L"MyAgent");
        CHttpConnection* httpcon=session.GetHttpConnection(L"localhost");
        CHttpFile* httpfile=httpcon->OpenRequest(CHttpConnection::HTTP_VERB_POST,L"i.php");
        CString headers=L"Content-Type: application/x-www-form-urlencoded";
        CString data=L"fname=MyName&age=17";
        httpfile->SendRequest(headers,(LPVOID)(LPCTSTR)data,data.GetLength());
        CString t=L"";
        UINT len=100;
        char buf[100];
        while(len>0){
        len=httpfile->Read(buf,100);
        if(len>0)t.Append(CString(buf),len);
        }
        AfxMessageBox(t);
        httpfile->Close();
        httpcon->Close();
        session.Close();

        Unfortunately, form is not being submitted. What I'm doing wrong? I'm trying to do this thing 3rd time now, I don't want to give up this time :-D Any help would be greatly appreciated.

        modified on Thursday, August 27, 2009 6:28 PM

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        You probably need to set the Content-Length header. If I were you, I'd download Fiddler[^] and use it to see what the difference is between the POST sent by the browser in response to that web form and the POST sent by your program.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        M 1 Reply Last reply
        0
        • S Stuart Dootson

          You probably need to set the Content-Length header. If I were you, I'd download Fiddler[^] and use it to see what the difference is between the POST sent by the browser in response to that web form and the POST sent by your program.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          M Offline
          M Offline
          msn92
          wrote on last edited by
          #4

          Thanks Stuart Dootson! I used Fiddler and looks like my program sending data in a different encoding. Browser is sending:

          fname=MyName
          age=17

          My program is sending:

          f[]n[]a[]m[]e[]=[]M[]y[]N[]a[]m[]e
          []a[]g[]e=[]1[]7[]

          (Meaning there is a square after each char) What should I do to send like WebBrowser does?

          S 1 Reply Last reply
          0
          • M msn92

            Thanks Stuart Dootson! I used Fiddler and looks like my program sending data in a different encoding. Browser is sending:

            fname=MyName
            age=17

            My program is sending:

            f[]n[]a[]m[]e[]=[]M[]y[]N[]a[]m[]e
            []a[]g[]e=[]1[]7[]

            (Meaning there is a square after each char) What should I do to send like WebBrowser does?

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            Use ASCII rather than Unicode strings, I guess?

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            M 1 Reply Last reply
            0
            • S Stuart Dootson

              Use ASCII rather than Unicode strings, I guess?

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              M Offline
              M Offline
              msn92
              wrote on last edited by
              #6

              Thank you again Stuart Dootson! It worked :) I used CStringA instead of CString. But, what if I want to send Unicode string? I'll be sending data in russian too. So is there way to send Unicode too?

              S 1 Reply Last reply
              0
              • M msn92

                Thank you again Stuart Dootson! It worked :) I used CStringA instead of CString. But, what if I want to send Unicode string? I'll be sending data in russian too. So is there way to send Unicode too?

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                Yes - you need to tell the system what encoding you're using. As far as I can see, that means adding an HTTP header OR something in the HTML document - this Wikipedia page[^] appears to have the details.

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                M 1 Reply Last reply
                0
                • S Stuart Dootson

                  Yes - you need to tell the system what encoding you're using. As far as I can see, that means adding an HTTP header OR something in the HTML document - this Wikipedia page[^] appears to have the details.

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  M Offline
                  M Offline
                  msn92
                  wrote on last edited by
                  #8

                  I used Fiddler to see what is the browser sending when I typed unicode text. Web Browser is converting the unicode text into hex values, like %F2%E8 =ти (russian) Let me post new thread for this is a different problem(Unicode to Hex conversion). My problem in this thread is solved. Stuart Dootson, thank you so much! :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups