• HttpWebRequest的简单使用


    新建新的空网站和一个default.aspx页面测试,实验例子:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpWebRequest web = (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/");  //创建一个请求
            web.Method = "GET";                                               //方式为get
            web.ContentType = "text/html;charset=UTF-8";                     //定义获取数据的类型
            HttpWebResponse response = (HttpWebResponse)web.GetResponse();   //获取响应
            string result = "";
            using (Stream stream = response.GetResponseStream())               //定义缓存,将数据读取出来
            {
                StreamReader sr = new StreamReader(stream);
                result = sr.ReadToEnd();
            }
            Label1.Text = result.ToString();
    
        }
    }

     二,客户端带参和带文件流的请求方法

            #region HTTP请求
            /// <summary>
            /// HTTP请求接口
            /// </summary>
            /// <param name="UploadFile"></param>
            /// <returns></returns>
            public ActionResult HZRequest2(HttpPostedFileBase UploadFile)
            {
                string key = "PKWodTrwuTXoMvRUZhUgyf";
                string url = "http://localhost:5832/Home/HZDistinguish?key=" + key + "";
                byte[] byt = new byte[UploadFile.InputStream.Length];
                UploadFile.InputStream.Read(byt, 0, (int)UploadFile.InputStream.Length);
                //初始化新的webRequst
                //1. 创建httpWebRequest对象
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                //2. 初始化HttpWebRequest对象
                webRequest.Method = "POST";
                webRequest.ContentType = "multipart/form-data";
                webRequest.ContentLength = byt.Length;
                //3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。)
                Stream newStream = webRequest.GetRequestStream();//创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面
    
                newStream.Write(byt, 0, byt.Length);
                newStream.Close();
                //4. 读取服务器的返回信息
                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                StreamReader stmRead = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string reapond = stmRead.ReadToEnd();
                response.Close();
                stmRead.Close();
                return Content(reapond);
            }
            #endregion

    服务器接收方法:

            public ActionResult HZDistinguish()
            {
                int lang = Request.TotalBytes;
                byte[] bytes = Request.BinaryRead(lang);
    
                Stream stream = Request.InputStream;
                string key = Request["key"];
            }    

     三,Post带参数的方法

            public static string Post(string url, Dictionary<string, object> dic)
            {
                StringBuilder str = new StringBuilder();
                foreach (KeyValuePair<string, object> kv in dic)
                {
                    string pkey = kv.Key;
                    object pvalue = kv.Value;
                    str.Append("&" + pkey + "=" + pvalue);
                }
                var data = Encoding.ASCII.GetBytes(str.ToString());
    
                //初始化新的webRequst
                //1. 创建httpWebRequest对象
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                //2. 初始化HttpWebRequest对象
                webRequest.Method = "Post";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.ContentLength = data.Length;
    
                using (var stream = webRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
                //4. 读取服务器的返回信息
                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
    
                StreamReader stmRead = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string result = stmRead.ReadToEnd();
                response.Close();
                stmRead.Close();
                return result;
            }
  • 相关阅读:
    java学习笔记——基于Robot类的屏幕分享
    Java实例——基于jsoup的简单爬虫实现(从智联获取工作信息)
    Java实例练习——基于UDP协议的多客户端通信
    java实例练习——基于TCP/IP协议的多客户端通信
    我个人的Java学习经验(一家之言)
    PHP mac localhost 环境下发送邮件
    php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
    php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
    程序员应该知道的13个设计技巧
    程序员应该知道的13个设计技巧
  • 原文地址:https://www.cnblogs.com/May-day/p/5512595.html
Copyright © 2020-2023  润新知