post数据小例

 private string getPageContent(string tastKeyword)
        {
            string htmlcontent = "";
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            string gethost = string.Empty;
            CookieContainer cc = new CookieContainer();
            string Cookiesstr = string.Empty;
            try
            {

                //第一次POST请求
                string postdata = @"username=autumn-iwom&password=trendschina&input=";//模拟请求数据
                string LoginUrl = "http://bbs.auto.ifeng.com/logging.php?action=login&loginsubmit=yes";
                request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
                request.Method = "POST";//数据提交方式为POST
                //模拟头
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabytes.Length;
                request.Referer = "bbs.auto.ifeng.com";
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                //提交请求
                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postdatabytes, 0, postdatabytes.Length);
                stream.Close();
                //接收响应
                response = (HttpWebResponse)request.GetResponse();
                //保存返回cookie
                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
                CookieCollection cook = response.Cookies;
                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
                Cookiesstr = strcrook;
                //取第一次GET跳转地址
                //StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
                //string content = sr.ReadToEnd();
                response.Close();
            }
            catch (Exception)
            {
                //第一次POST出错;
            }
            try
            {
                gethost = "http://bbs.auto.ifeng.com/search.php?ascdesc=desc&before=&formhash=89f0be48&orderby=lastpost&searchsubmit=true&srchfid%5B%5D=all&srchfilter=all&srchfrom=0&srchtxt={0}&srchtype=title&srchuname=&st=on"; //第一次GET地址
                gethost = string.Format(gethost, tastKeyword);
                request = (HttpWebRequest)WebRequest.Create(gethost);
                request.Method = "POST";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + Cookiesstr);
                request.CookieContainer = cc;
                request.AllowAutoRedirect = true;
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie   
                Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
                //取再次跳转链接   
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                htmlcontent = sr.ReadToEnd();
                request.Abort();
                sr.Close();
                response.Close();
            }
            catch (Exception)
            {
                //第二次POST出错   
            }
            return htmlcontent;
        }

<think>我们正在讨论HTTP POST请求的原理和使用方法。根据引用[1]和[2],POST请求将数据放在HTTP请求的正文(entity body)中,而不是URL中。同时,引用[3]展示了一个简单的POST请求的JavaScript代码示。 原理: 1. POST请求的参数传输是在HTTP标题的不同部分(entity body)中进行的。 2. 通常用于表单提交,需要设置Content-Type头,如application/x-www-form-urlencoded(表单默认提交类型),或者multipart/form-data(用于文件上传)等。 3. 相对于GET请求,POST请求的数据不会暴露在URL中,因此更安全(但实际安全性需要配合HTTPS),且可以传输较大量的数据(因为URL长度有限制)。 使用方法(以JavaScript为,参考引用[3]): 1. 创建XMLHttpRequest对象。 2. 使用open方法初始化一个POST请求,指定URL和异步标志(true表示异步)。 3. 设置必要的请求头(如Content-Type)。 4. 调用send方法发送数据数据格式为字符串(如键值对:'username=Iris&password=12345')。 另外,引用[4]提到了Unity3D中的HTTP通信,使用WWW类进行POST请求,但主要介绍了异步处理(使用yield)和常用属性(如text和error)。 因此,我们可以整理如下: 原理: - POST请求将数据放在HTTP请求的正文中,不会像GET请求那样附加在URL后面(引用[1][2])。 - POST请求需要设置Content-Type头,以告知服务器数据的格式(引用[1])。 - POST请求可以传输比GET请求更多的数据,因为URL长度有限制(引用[2])。 使用方法: - 在客户端脚本中(如JavaScript),可以使用XMLHttpRequest对象发送POST请求(引用[3])。 - 在Unity3D中,可以使用WWW类(但注意,Unity中推荐使用UnityWebRequest替代WWW)来发送POST请求,并利用协程处理异步(引用[4])。 我们将按照以下结构回答: 1. 原理部分(包括与GET的对比) 2. 使用方法(包括代码示) 3. 注意事项(如Content-Type设置) 同时,根据要求,我们需要生成相关问题。 注意:引用标记需要添加在引用内容的末尾。 </think>### HTTP POST数据的原理及使用方法 #### **一、工作原理** 1. **数据传输位置**: POST请求的数据位于HTTP请求的**实体主体(entity body)** 中,而非URL中。这避免了数据直接暴露在地址栏(与GET请求的关键区别)[^2]。 2. **数据格式与编码**: - 默认使用 `application/x-www-form-urlencoded` 格式,将数据编码为键值对(如 `username=Iris&password=12345`)[^1]。 - 支持其他格式(如JSON、二进制流),需通过 `Content-Type` 头显式声明(如 `application/json`)。 3. **设计目标**: 主要用于**表单提交**,支持较大数据量传输(无URL长度限制),适用于敏感信息(密码、文件上传等)[^1][^2]。 #### **二、使用方法示** ##### 1. **原生JavaScript实现**(引用[3]) ```javascript // 创建请求对象 var xhr = new XMLHttpRequest(); // 初始化POST请求(异步模式) xhr.open('POST', '/submit-data', true); // 设置请求头(声明数据格式) xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); // 发送数据(键值对格式) xhr.send('username=Iris&password=12345'); ``` ##### 2. **Unity3D实现**(引用[4]) ```csharp IEnumerator PostData() { WWWForm form = new WWWForm(); form.AddField("username", "Iris"); // 添加字段 form.AddField("password", "12345"); WWW www = new WWW("http://example.com/api", form); yield return www; // 异步等待响应 if (www.error == null) { Debug.Log("响应数据: " + www.text); // 获取返回结果 } else { Debug.LogError("请求失败: " + www.error); } } ``` #### **三、关键注意事项** 1. **安全性**: POST不加密数据!敏感操作需配合HTTPS[^2]。 2. **性能影响**: 因需传输完整请求体,性能略低于GET,但更安全且支持复杂数据[^1]。 3. **幂等性**: POST请求**非幂等**(多次调用可能产生不同结果),适合创建/更新操作(如提交订单)[^1]。 #### **四、适用场景** - 表单提交(登录、注册) - 文件上传(如图片、视频) - 敏感数据传输(密码、支付信息) - 大数据传输(JSON/XML格式的API请求) ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值