Link to home
Start Free TrialLog in
Avatar of jui2ce
jui2ce

asked on

Post FORM Automatically to another web site not on the same server. USing Asp.net/VB

I would like to post to
http://MyOtherPage.com/posthere/

What I would like to post is
Action=Login
Lang=en
User=Myuser
Password=Mypassword

Avatar of martie_11
martie_11
Flag of United States of America image

Hello jui2ce,

Try this:

using System.Text;
...
ASCIIEncoding encoding = new ASCIIEncoding();
string POSTData = "Action=Login&Lang=en&User=Myuser&Password=Mypassword";

byte[]  data = encoding.GetBytes(POSTData);

// Prepare web request...
HttpWebRequest myRequest =
      (HttpWebRequest)WebRequest.Create("http://MyOtherPage.com/posthere/");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();

If you want a response, you need to do the following (ie.. on the same cs page):

// GET THE RESPONSE FROM THE SERVER
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
using ( StreamReader sr = new StreamReader(myResponse.GetResponseStream()) )
{
      Response.Charset = "";
      Response.ContentType = "text/HTML";
      Response.Write(sr.ReadToEnd());  // Prints response to current page
}

Hope that helps!
Avatar of jui2ce
jui2ce

ASKER

I know this is bad but I'm really bad at transfering  C to VB could you help?
ASKER CERTIFIED SOLUTION
Avatar of martie_11
martie_11
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jui2ce

ASKER

When I use the Code aboce I get
Name 'HttpWebRequest is not declared'

It is refering to
HttpWebRequest myRequest =
add:

Imports System.Net;