向post请求中写入数据,最终保存在了HttpWebRequest.Params中
2015-01-27 09:46
351 查看
一、向post请求中写入数据,最终保存在了HttpWebRequest.Params中:
1)如果存入的是IDictionary类型的字符串变量,如:“username=administrator”,则key=value;
2)如果写入的是string类型的变量,如"username",则key=null,value=username;
二、从request中取出写入的data数据(比如:xml等)的另一种方法:从流中获取(仅一次)
三、实现数据在IHttpModule和IHttpHandler之间(process项目和web项目之间)数据的组织和共享的键/值集合,保存在context.Items中
四、从Items集合中取出数据
1)如果存入的是IDictionary类型的字符串变量,如:“username=administrator”,则key=value;
2)如果写入的是string类型的变量,如"username",则key=null,value=username;
protected void btnLogin_Click(object sender, EventArgs e) { string Url = "http://localhost:18472/DataRequest.aspx"; string contentType = "application/x-www-form-urlencoded"; string username = "administrator"; string password = "admin"; IDictionary<string,string> param=new Dictionary<string,string>(); param.Add("username",username); param.Add("password",password); int i = 0; StringBuilder sb=new StringBuilder(); foreach (var key in param.Keys) { if (i>0) { sb.AppendFormat("&{0}={1}", key, param[key]); } else { sb.AppendFormat("{0}={1}", key, param[key]); } i++; } //string content = SendPost(sb.ToString(),Url,contentType); string content = SendPost(username,Url,contentType); } /// <summary> /// /// </summary> /// <param name="data">写入流中的数据,最后将保存在request.params中,是Idictionary类型的变量;如果是没有等号的string类型,key=null,value=string</param> /// <param name="url">请求的url</param> /// <param name="contentType">请求的内容类型</param> /// <returns></returns> public static string SendPost(string data, string url, string contentType) { string content = string.Empty; HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest; httpWebRequest.AllowAutoRedirect = true; httpWebRequest.Method = "POST"; httpWebRequest.ContentType = contentType; using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream())) { sw.Write(data); } HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), System.Text.Encoding.Default)) { content = sr.ReadToEnd(); } //httpWebRequest.EndGetResponse; return content; }
二、从request中取出写入的data数据(比如:xml等)的另一种方法:从流中获取(仅一次)
string content=""; using (Stream stream = HttpContext.Current.Request.InputStream) { using (StreamReader sr = new StreamReader(stream, Encoding.UTF8)) { content=sr.ReadToEnd(); } }
三、实现数据在IHttpModule和IHttpHandler之间(process项目和web项目之间)数据的组织和共享的键/值集合,保存在context.Items中
string XML=content; HttpContext context=HttpContext.Current; context.Items.Add("XML", XML);
四、从Items集合中取出数据
string XML=context.Items["XML"].ToString();
相关文章推荐
- HttpWebRequest POST 数据时请求头多了一行Expect: 100-continue,少了数据行
- HttpWebRequest post请求获取webservice void数据信息
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- C#POST数据,HttpWebRequest请求页面,HttpWebResponse返回数据
- C# HttpWebRequest Post Get 请求数据
- silverlight HttpWebRequest post请求数据
- HttpWebRequest POST 数据时请求头多了一行Expect: 100-continue,少了数据行
- HttpWebRequest POST 数据时请求头多了一行Expect: 100-continue,少了数据行
- 用C#使用HttpWebRequest Post数据时如何保持Session
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- 用HttpWebRequest做POST请求时返回Http 417 expectation failed
- HttpWebRequest post数据[xgluxv]
- 用httpwebrequest/httpwebresponse向服务器POST发送及接收数据
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- 使用HttpWebRequest发送自定义POST请求
- dotnet 使用HttpWebRequest向Https Post数据时,需要证书确认的解决方法
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- 用C#使用HttpWebRequest Post数据时如何保持Session