philzhou

导航

使用HttpWebRequest模拟Web请求

http post 请求:

          string uri = "http://www.XXXXXXX.com/Default_Login.aspx?usercode=admin&password=123";

            // Get an new instance from factory method.

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            HttpWebResponse response =(HttpWebResponse) request.GetResponse();

            Stream stream = response.GetResponseStream();

            StreamReader reader = new StreamReader(response.GetResponseStream());

            string resultText = reader.ReadToEnd();

            Console.WriteLine(resultText);

            reader.Close();

            stream.Close();

            Console.Read();

  

 

Http get 请求:

    string uri ="http://www.XXXXXX.com/login.jsp";

            string formdata = "loginid=philzhou&userpassword=777777";

            ASCIIEncoding encoding=new ASCIIEncoding(); 

            byte[] encodeddata = encoding.GetBytes(formdata);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method="Post";

            request.ContentType = "application/x-www-form-urlencoded";

            request.ContentLength = encodeddata.Length;

            Stream requestStream = request.GetRequestStream();

    // write form data to request stream.

            requestStream.Write(encodeddata, 0, encodeddata.Length);

            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream stream = response.GetResponseStream();

            StreamReader reader = new StreamReader(stream,Encoding.GetEncoding("gb2312"));

            string result = reader.ReadToEnd();

            Console.WriteLine(result);

            reader.Close();

            stream.Close();

            Console.Read();

posted on 2011-06-16 16:05  philzhou  阅读(858)  评论(1编辑  收藏  举报