Submit a web form: What I'm doing wrong? [modified]
-
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
-
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
-
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
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
-
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
Thanks Stuart Dootson! I used Fiddler and looks like my program sending data in a different encoding. Browser is sending:
fname=MyName
age=17My 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?
-
Thanks Stuart Dootson! I used Fiddler and looks like my program sending data in a different encoding. Browser is sending:
fname=MyName
age=17My 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?
Use ASCII rather than Unicode strings, I guess?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Use ASCII rather than Unicode strings, I guess?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
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?
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
-
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
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! :)