using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace ClassLibrary1
{
public static class FormPostHelper
{
public static string FormPost()
{
string result = string.Empty;
//this sets up the request
Uri postUri = new Uri("http://www.domain.com/pagetopostto.htm");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUri);
//here you define the form fields
string xmlToPass = "FormField=Value";
byte[] bytes = Encoding.UTF8.GetBytes(xmlToPass);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = xmlToPass.Length;
//open a stream to the target URL
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
//submit the POST, get the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
System.IO.StreamReader reader = new StreamReader(responseStream);
result = reader.ReadToEnd();
reader.Close();
}
return result;
}
}
}
Sunday, July 27, 2008
Simple Form Post Example
There's plenty of sites out there that offer services through form POST. Here's a really quick and dirty example of how to submit and receive:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment