2 u011954277 u011954277 于 2016.03.11 16:51 提问

c# winfrom post请求网址带多个参数的问题 请看下面代码
 string strURL = "http://www.cqjg.gov.cn/newwww/c7/clwz.asp";  
            System.Net.HttpWebRequest request;  
            request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);  
            //Post请求方式  
            request.Method = "POST";  
            //内容类型  
            request.ContentType = "application/x-www-form-urlencoded";  
            //参数经过URL编码  
            string paraUrlCoded = System.Web.HttpUtility.UrlEncode("Name");  
            paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarId .Text.Trim());  
            paraUrlCoded +="& "+ System.Web.HttpUtility.UrlEncode("Age");  
            paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarNumber.Text.Trim());  
            byte[] payload;  
            //将URL编码后的字符串转化为字节  
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);  
            //设置请求的ContentLength   
            request.ContentLength = payload.Length;  
            //获得请求流  
            Stream writer = request.GetRequestStream();  
            //将请求参数写入流  
            writer.Write(payload, 0, payload.Length);  
            //关闭请求流  
            writer.Close();  
            System.Net.HttpWebResponse response;  
            //获得响应流  
            response = (System.Net.HttpWebResponse)request.GetResponse();  
            System.IO.Stream s;  
            s = response.GetResponseStream();  
            string StrDate = "";  
            string strValue = "";  
            StreamReader Reader = new StreamReader(s,Encoding.Default);  
            while ((StrDate= Reader.ReadLine())!=null)  
            {  
                strValue += StrDate+"\r\n";  
            }  
            // txtInfo.Text = strValue;  
            txtInfo.Text= FindContent(strValue);  

如果浏览器请求的 正文是 Name=小明那么

string paraUrlCoded = System.Web.HttpUtility.UrlEncode("Name");

paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarId .Text.Trim());

就能成功

如果是Name=小明&type=1
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("Name");

paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarId .Text.Trim());

paraUrlCoded +="& "+ System.Web.HttpUtility.UrlEncode("type");

paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(1);
这样就会失败提交不成功

                    还有问下如果请求主题是{"Item":{"Name":"小明"}}这样子或者是{"Name":"test"}这样子的话我该怎么传呢?   麻烦请教下了

1个回答

caozhy
caozhy   Ds   Rxr 2016.03.12 02:53
已采纳

fiddler看下,一个是中文urlencode转码是否正确,一个是有没有多余的回车和空格, 一个是服务器返回了什么

u011954277
u011954277 服务器直接返回一串乱码
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!