i want to post a form on other website using my code.

Posted by Asad1605 under ASP.NET on 8/25/2010 | Points: 10 | Views : 1371 | Status : [Member] | Replies : 2
I want to submit a form residing on other domain from my asp.net appliocation and then get result in my asp.net page(response). I have tried the way in which HTTPWebRequest and HTTPWebResponse objects were used, but no success.
For more clarity, i want to send a container no. to a website providing container tracking and get the result page html in my control.
There is no authentication there. Webite url is "http://www.concorindia.com/containerquery.aspx". Is there any other way to accomplish this?

I am using the code:

1. string url = "http://www.concorindia.com/containerquery.aspx";
3. WebRequest request = WebRequest.Create(url);
5. request.Method = "POST";
7. string postData = "contno=123456&CONTButton1='Submit Query'";
9. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
10. request.ContentType = "application/x-www-form-urlencoded";
11. request.ContentLength = byteArray.Length;
12. Stream dataStream = request.GetRequestStream();
13. dataStream.Write(byteArray, 0, byteArray.Length);
14. dataStream.Close();
16. WebResponse response = request.GetResponse();
17. dataStream = response.GetResponseStream();
18. StreamReader reader = new StreamReader(dataStream);
19. string responseFromServer = reader.ReadToEnd();
20. response.Wtrite(responseFromServer);


Posted by: SheoNarayan on: 8/25/2010 [Administrator] HonoraryPlatinum | Points: 25

Hi Asad1605,

I modified your code a bit and its working for me. For testing purpose I kept a TextBox in my aspx page

<asp:TextBox ID="txtBox1" runat="server" TextMode="MultiLine" Rows="20" Columns="100" />

and in my Code behind page page load, I wrote following code and it gave me complete html page from the website you mentioned.

Use following namespace apart from default that comes in the code behind.
using System.Net;
using System.IO;
using System.Text;

protected void Page_Load(object sender, EventArgs e)

// EmailSender.SendEmail("info@itfunda.com", "", "", "Subject", "Body", System.Net.Mail.MailPriority.Normal, false);

string url = "http://www.concorindia.com/containerquery.aspx";

WebRequest request = WebRequest.Create(url);

request.Method = "POST";

string postData = "contno=123456&CONTButton1='Submit Query'";

byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);

WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
txtBox1.Text = responseFromServer;

There were some typo in your code, hope this will help you.

Thanks for asking question.

Sheo Narayan

Asad1605, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Asad1605 on: 8/25/2010 [Member] Starter | Points: 25

Thanx Sheo.
The html you are getting is of the page that is to be posted, not of the result page. I want to post this page with the values stored in "postData" variable. i am trying to get the html of page that will come after this page.
You can see the page "http://www.concorindia.com/containerquery.aspx" in ur browser to understand what i want.


Asad1605, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response