I want to basically run an url which i would be generating behind the scenes without actually displaying it in browser to user...i guess i could use HTTPWebRequest or maybe something similiar to curl?...but i need to really just basically visit/run the generated url? how can i do that ?
That's when your own server can't post to the internet or is behind a firewall. This way you leverage the client's browser to do the deed.
ggonsalv
2010-07-26 23:43:08
+1
A:
There is a good example found here http://www.netomatix.com/httppostdata.aspx
I copied and pasted the example method used to browse to a url:
private void OnPostInfoClick(object sender, System.EventArgs e)
{
string strId = UserId_TextBox.Text;
string strName = Name_TextBox.Text;
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
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();
}
Roberto Sebestyen
2010-07-26 19:36:43
You know, WebClient ( http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx ) is a much nicer way to send POST data.
Matti Virkkunen
2010-07-26 19:38:01
this one is for sending data to the url only. you can omit a few of these lines if you dont want to send data, or use GET instead of POST and send data using the query string. If you want to read the results, you might have add additional lines between the Write and Close. If you just want to 'visit' you can simplify this further.
kinjal
2010-07-26 19:40:34
@Matti yes WebClient seems easier to use. Either way there is many ways to skin the cat. @kinjal Obviously it can be simplified, it was meant to be as a quick example. I did say that I copied and pasted from netomatix.com for the sole purpose of saving you from having to visit the link to see the sample code, so I haven't bothered 'simplifying' it.
Roberto Sebestyen
2010-07-26 19:54:53
+1
A:
I am guessing you are looking for something like this?
Dim request = WebRequest.Create(strUrl)
request.Method = "POST"
request.ContentType = "text/xml" 'change to whatever you need
Use the following part optionally to create the body of the request if you are sending this to a web service that needs this, for example
Using sw As New StreamWriter(request.GetRequestStream())
sw.WriteLine(HtmlOrXml)
End Using
Get the response:
Dim response = CType(request.GetResponse(), HttpWebResponse)
You can then use StreamReader to read the response. You can find more about the classes used above on MSDN.
Antony Highsky
2010-07-26 19:39:40