模拟Http请求

 
public ActionResult Index()
        {
            var model = base.RenderOutPutModel();

            //string url = "http://localhost:46145/ServerQueue";

            //SendDTO SendDTO = new SendDTO();
            //SendDTO.CallbackId = "sdsfasdfsdfsadfsdfsad22222";
            //SendDTO.Account = "123";
            //SendDTO.Password = "123";
            //SendDTO.CallbackUrl = "http://localhost:46145/DataExchange/DataExchange?id=&ss=";

            //HttpHelper.PostDataHtml(url, System.Web.HttpUtility.UrlEncode(JsonConvert.SerializeObject(SendDTO)));//编码+序列化



            return View(model);
        }

 

 

 

 /// <summary>
        /// HttpWebRequest 通过Post
        /// </summary>
        /// <param name="url">URI</param>
        /// <param name="postData">post数据</param>
        /// <returns></returns>
        public static void PostDataHtml(string url, string body)
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[] postData = encoding.GetBytes(body);
            var uri = new Uri(url);

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

            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = postData.Length;
            myRequest.PreAuthenticate = true;
            myRequest.AllowWriteStreamBuffering = false;
            myRequest.SendChunked = false;
            myRequest.KeepAlive = true;
            myRequest.Timeout = int.MaxValue;
            myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            myRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes("admin:admin")));

            //身份凭证
            CredentialCache cc = new CredentialCache();
            cc.Add(uri, "Basic", new NetworkCredential("admin", "admin"));
            myRequest.Credentials = cc;

            //发送数据
            Stream newStream = myRequest.GetRequestStream();
            // Send the data.
            newStream.Write(postData, 0, postData.Length);
            newStream.Flush();
            newStream.Close();

            //返回响应
            HttpWebResponse myResponse;
            myResponse = (HttpWebResponse)myRequest.GetResponse();

            if (myResponse != null && myResponse.StatusCode == HttpStatusCode.OK)
            {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

                var str = reader.ReadToEnd();
            }
        }

接收方法

    public JsonResult Index()
        {
            byte[] byts = new byte[Request.InputStream.Length];
            Request.InputStream.Read(byts, 0, byts.Length);
            string data = System.Text.Encoding.Default.GetString(byts);
            data = Server.UrlDecode(data);

            SendDTO dto = JsonConvert.DeserializeObject<SendDTO>(data);
        }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值