向Url发送post请求传递参数

简介:
        #region  向Url发送post请求,返回网站响应内容
        /// <summary>
        /// 向Url发送post请求,返回网站响应内容
        /// </summary>
        /// <param name="postData">发送数据</param>
        /// <param name="uriStr">接受数据的Url</param>
        /// <param name="action">更新操作</param>
        /// <returns>返回网站响应内容</returns>
        public static string RequestPost(string postData, string uriStr, string action)
        {
            HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create(uriStr);
            StringBuilder postContent = new StringBuilder();
            Encoding myEncoding = Encoding.GetEncoding("gb2312");
            postContent.Append(HttpUtility.UrlEncode(" message", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode(postData, myEncoding));
            postContent.Append("&");
            postContent.Append(HttpUtility.UrlEncode(" type", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode(" sync", myEncoding));
            postContent.Append("&");
            postContent.Append(HttpUtility.UrlEncode(" action", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode(action, myEncoding));


            byte[] data = Encoding.ASCII.GetBytes(postContent.ToString());
            requestScore.Method = "Post";
            requestScore.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
            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


网站接收数据:

            string message = Request.Form["message"];
            string type = Request.Form["type"];
            string action = Request.Form["action"];

目录
相关文章
|
26天前
|
前端开发 JavaScript
前端JS截取url上的参数
文章介绍了两种前端JS获取URL参数的方法:手动截取封装和使用URLSearchParams。
31 0
|
3月前
|
缓存 网络协议 Java
(六)网络编程之化身一个请求感受浏览器输入URL后奇妙的网络之旅!
在浏览器上输入一个URL后发生了什么? 这也是面试中老生常谈的话题,包括网上也有大量关于这块的内容。
105 2
|
2月前
|
数据采集 人工智能 监控
【Azure 应用程序见解】Application Insights Java Agent 3.1.0的使用实验,通过修改单个URL的采样率来减少请求及依赖项的数据采集
【Azure 应用程序见解】Application Insights Java Agent 3.1.0的使用实验,通过修改单个URL的采样率来减少请求及依赖项的数据采集
|
2月前
|
前端开发 API
【API管理 APIM】APIM中如何配置使用URL路径的方式传递参数(如由test.htm?name=xxx 变为test\xxx)
【API管理 APIM】APIM中如何配置使用URL路径的方式传递参数(如由test.htm?name=xxx 变为test\xxx)
|
3月前
|
缓存 安全 Web App开发
Chrome插件实现问题之网络进程接收到URL请求后会如何解决
Chrome插件实现问题之网络进程接收到URL请求后会如何解决
|
2月前
|
开发框架 前端开发 .NET
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
|
2月前
|
Java
JAVA 获取 URL 指定参数的值
JAVA 获取 URL 指定参数的值
42 0
|
4月前
|
域名解析 存储 缓存
HTTP请求流程概览:浏览器构建请求行含方法、URL和版本;检查缓存;解析IP与端口
【6月更文挑战第23天】 HTTP请求流程概览:浏览器构建请求行含方法、URL和版本;检查缓存;解析IP与端口;TCP连接(HTTP/1.1可能需排队);三次握手;发送请求头与体;服务器处理并返回响应;TCP连接可能关闭或保持;浏览器接收并显示响应,更新缓存。HTTP版本间有差异。
75 5
|
3月前
|
JavaScript 前端开发 数据格式
URL编码【详解】——Javascript对URL进行编码解码的三种方式的区别和使用场景,axios请求拦截器中对get请求的参数全部进行URL编码
URL编码【详解】——Javascript对URL进行编码解码的三种方式的区别和使用场景,axios请求拦截器中对get请求的参数全部进行URL编码
175 0
|
3月前
|
JavaScript
js 获取并解析 url 中参数的三种方法
js 获取并解析 url 中参数的三种方法
239 0