文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>使用HttpWebRequest模拟Web请求

使用HttpWebRequest模拟Web请求

时间:2011-06-16  来源:philzhou

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();

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载