模拟Http请求
来源:互联网 发布:国家规划教材 知乎 编辑:程序博客网 时间:2024/06/11 01:10
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(); } }
阅读全文
0 0
- HttpWebRequest 模拟Http请求
- telnet模拟Http请求
- Jave 模拟 http 请求
- http请求模拟
- telnet模拟HTTP请求
- 模拟http 请求
- PHP模拟http请求
- HTTP 模拟post请求
- 模拟发送http请求
- socket模拟http请求
- 模拟HTTP请求
- c#模拟http请求
- C# 模拟 Http 请求
- java模拟http请求
- java模拟HTTP请求
- linux 模拟http请求
- php模拟http请求
- PHP模拟HTTP请求
- 算法学习开源代码
- jQuery定义插件的方法
- MFC不同类之间的函数调用
- Binary String Matching(南阳理工OJ)
- 设计模式——总概述
- 模拟Http请求
- 解决使用枚举串口类而造成无法无法解析的外部符号的问题
- hdu1384—Intervals
- sklearn.model_selection.KFold
- 欢迎使用CSDN-markdown编辑器
- 设计模式——单例模式
- bug 整理
- Android Studio 运行java程序
- 常见TCP/IP、HTTP协议以及三次握手和四次挥手