WebRequest 调用方法

来源:互联网 发布:电脑免费恢复数据软件 编辑:程序博客网 时间:2024/06/11 10:14

string url = "http://localhost:3520/WebSite1/Default2.aspx";
        Encoding encoding = Encoding.GetEncoding("gb2312");//设置发送数据时的编码格式

        string jpydpostData = "code=1";//充值状态
        jpydpostData += ("&strinfo=" + Server.UrlEncode(str) + "");//订单号

        byte[] data = encoding.GetBytes(jpydpostData);

        // 准备网页请求。...
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
        myRequest.Method = "post";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.ContentLength = data.Length;
        Stream newStream = myRequest.GetRequestStream();

        // 发送数据
        newStream.Write(data, 0, data.Length);
        newStream.Close();

        // 获得返回的数据
        HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
        StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
        string content = reader.ReadToEnd();

0 0
原创粉丝点击