模拟post请求
来源:互联网 发布:西安交通大学网络导航 编辑:程序博客网 时间:2024/06/01 09:13
- #region 向Url发送post请求
- /// <summary>
- /// 向Url发送post请求
- /// </summary>
- /// <param name="postData">发送数据</param>
- /// <param name="uriStr">接受数据的Url</param>
- /// <returns>返回网站响应请求的回复</returns>
- public static string RequestPost(string postData, string uriStr)
- {
- HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create(uriStr);
- ASCIIEncoding encoding = new ASCIIEncoding();
- byte[] data = encoding.GetBytes(postData);
- requestScore.Method = "Post";
- requestScore.ContentType = "application/x-www-form-urlencoded";
- requestScore.ContentLength = data.Length;
- requestScore.KeepAlive = true;
- Stream stream = requestScore.GetRequestStream();
- stream.Write(data, 0, data.Length);
- stream.Close();
- HttpWebResponse responseSorce;
- try
- {
- responseSorce = (HttpWebResponse)requestScore.GetResponse();
- }
- catch (WebException ex)
- {
- responseSorce = (HttpWebResponse)ex.Response;//得到请求网站的详细错误提示
- }
- StreamReader reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.UTF8);
- string content = reader.ReadToEnd();
- requestScore.Abort();
- responseSorce.Close();
- responseSorce.Close();
- reader.Dispose();
- stream.Dispose();
- return content;
- }
- #endregion
网站得到Post过来的数据:
- /// <summary>
- /// 得到程序post过来的数据
- /// </summary>
- /// <returns></returns>
- private string GetPostContent()
- {
- string postStr = string.Empty;
- Stream inputStream = Request.InputStream;
- int contentLength = Request.ContentLength;
- int offset = 0;
- if (contentLength > 0)
- {
- byte[] buffer = new byte[contentLength];
- for (int i = inputStream.Read(buffer, offset, contentLength - offset); i > 0; i = inputStream.Read(buffer, offset, contentLength - offset))
- {
- offset += i;
- }
- UTF8Encoding encoding = new UTF8Encoding();
- postStr = encoding.GetString(buffer);
- }
- return postStr;
- }
0 0
- 模拟Post请求
- HTTP 模拟post请求
- C#模拟post请求
- Java模拟post请求
- asp模拟post请求
- cURL模拟POST请求
- 模拟post请求
- 终端模拟post请求
- CSRF模拟post请求
- 模拟POST请求
- CURL模拟post请求
- JAVA模拟POST请求
- 模拟POST请求
- Java模拟Post请求
- JAVA模拟post请求
- curl模拟post请求
- HttpClient模拟Post请求
- curl模拟post请求
- js实现 图片随鼠标移动,随滚轮放大缩小
- Jetty问题解决汇总
- Making AI Fun: When Good Enough is Good Enough
- Hive启动metastore的出错org.apache.thrift.transport.TTransportException
- 为啥REST如此重要
- 模拟post请求
- Java基础笔记 – 内部类 静态内部类 成员内部类 局部内部类 匿名内部类anonymous inner classes
- 全志a13开发板——建立一个可以启动linux的SD卡
- WindowsStations窗口站
- 军训 (水题)
- Hibernate Validator学习
- ASP.NET MVC 3 Razor 视图引擎 基本语法
- 记录
- Kinect V2 开发专题(1)软硬件需求与安装测试