usingSystem;usingSystem.IO;usingSystem.Collections;usingSystem.Data;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;usingSystem.Xml.Linq;usingSystem.Net;usingSystem.Text;namespacewebapplication1{[WebService (Namespace="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] Public classHandler1:ihttphandler { Public voidProcessRequest (HttpContext context) {HttpWebRequest req= (HttpWebRequest) httpwebrequest.create ("http://fanyi.baidu.com/transcontent"); Encoding Encoding=Encoding.UTF8; stringparam ="ie=utf-8&source=txt&query=hello&t=1327829764203&token=8a7dcbacb3ed72cad9f3fb079809a127 &from=auto&to=auto"; //encoding. GetBytes (postdata); byte[] bs =Encoding.ASCII.GetBytes (param); stringResponseData =String.Empty; Req. Method="POST"; Req. ContentType="application/x-www-form-urlencoded"; Req. ContentLength=BS. Length; using(Stream Reqstream =req. GetRequestStream ()) {Reqstream.write (BS,0, BS. Length); Reqstream.close (); } using(HttpWebResponse response =(HttpWebResponse) req. GetResponse ()) {using(StreamReader reader =NewStreamReader (response. GetResponseStream (), encoding)) {ResponseData=Reader. ReadToEnd (). ToString (); } context. Response.Write (ResponseData); } } Public BOOLisreusable {Get { return false; } } }}
An example of a HttpWebRequest POST request