一、向post请求中写入数据,最终保存在了HttpWebRequest.Params中:

  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 = ;
StringBuilder sb=new StringBuilder();
foreach (var key in param.Keys)
{
if (i>)
{
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();

向post请求中写入数据,最终保存在了HttpWebRequest.Params中的更多相关文章

  1. PHP连接sqlserver的两种方法,向sqlserver2000中写入数据,中文乱码

    项目环境是php5.3.28 项目用的ThinkPHP3.2.3  已经mysql5.5数据库,要和另一个项目对接,需要连接sqlsever2000数据库进行一些操作. 第一种用php自带扩展连接数据 ...

  2. 通过I2C总线向EEPROM中写入数据,记录开机次数

    没买板子之前,用protues画过电路图,实现了通过i2c总线向EEPROM中写入和读出数据. 今天,在自己买的板子上面写关于i2c总线的程序,有个地方忘了延时,调程序的时候很蛋疼.下面说说我对I2c ...

  3. 【转】从QDataStream向QByteArray中写入数据时的注意点(QT)

    最近发现从QDataStream向QByteArray中写入数据常常是写不进去的,通过查看QT的源码: QDataStream &operator>>(QDataStream &a ...

  4. POI往word模板中写入数据

    转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977  版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...

  5. Verilog利用$fdisplay命令往文件中写入数据

    最近在做的事情是,用FPGA生成一些满足特定分布的序列.因此为了验证我生成的序列是否拥有预期的性质,我需要将生成的数据提取出来并且放到MATLAB中做数据分析. 但是网上的程序很乱,表示看不懂==其实 ...

  6. 复制excel表,往excel表中写入数据

    import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import jav ...

  7. POI向Excel中写入数据及追加数据

    import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...

  8. 计算机二级-C语言-程序填空题-190117记录-对文件的处理,复制两个文件,往新文件中写入数据。

    //给定程序的功能是,调用函数fun将指定源文件中的内容赋值到指定目标文件中,复制成功时函数返回1,失败时返回0,把复制的内容输出到终端屏幕.主函数中源文件名放在变量sfname中,目标文件名放在变量 ...

  9. sql语句 怎么从一张表中查询数据插入到另一张表中?

    sql语句 怎么从一张表中查询数据插入到另一张表中?  ----原文地址:http://www.phpfans.net/ask/MTc0MTQ4Mw.html 比如我有两张表 table1 字段 un ...

随机推荐

  1. jquery json 操作(转)

    原文:http://www.vososo.com/?p=919 Jquery-json 是 jQuery 的一个插件,可轻松实现对象和 JSON 字符串之间的转换.可序列化 JavaScript 对象 ...

  2. 使用TypeScript开发程序

    简介 TypeScript一直发展不错,我们公司在开发新功能时,考虑到程序的可维护性,使用了TypeScript编写浏览器上的程序,我们是从零开始使用TypeScript,甚至我连javascript ...

  3. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  4. 解决apache AH01630: client denied by server configuration错误

    昨天给公司配置了apache-2.4.9的版本,今天他们要求把虚拟主机配置起好放网站程序,在修改apache-2.4.9的配置文件中,我发现了2.4.x跟以前的2.2.x里面的很多配置都不一样了,比如 ...

  5. 强大的Resharp插件

    使用VS有段时间了,一直深深的折服于其强大的功能.之前一直听说有Resharp这个工具,小猪一直也没有太在意.直到今天…… 下载安装: http://www.jetbrains.com/resharp ...

  6. eclipse debug时老提示edit source lookup path解决方案

    用myeclipse debug web应用的时候,总提示edit source lookup path,每次都得手动选择项目,费时费力.在网上终于找到了方法. 搬运:http://www.educi ...

  7. uva 1210

    #include<iostream> #include<cstring> using namespace std; + ; bool notprime[MAXN];//值为fa ...

  8. DataGridView复选框实现单选功能(二)

    双击DataGridView进入事件 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventA ...

  9. 2.精通前端系列技术之JavaScript模块化开发 seajs(一)

    在使用seajs模块化开发之前,直接在页面引用js会容易出现冲突及依赖相关的问题,具体问题如下 问题1:多人开发脚本的时候容易产生冲突(比如全局参数冲突,方法名冲突),可以使用命名空间降低冲突,不能完 ...

  10. [转载]Matrix类的使用

    2013-12-18 11:31:00 转载自: http://www.cnblogs.com/mmy0925/archive/2013/01/22/2871009.html 在Android中,对图 ...