向post请求中写入数据,最终保存在了HttpWebRequest.Params中
一、向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中的更多相关文章
- PHP连接sqlserver的两种方法,向sqlserver2000中写入数据,中文乱码
项目环境是php5.3.28 项目用的ThinkPHP3.2.3 已经mysql5.5数据库,要和另一个项目对接,需要连接sqlsever2000数据库进行一些操作. 第一种用php自带扩展连接数据 ...
- 通过I2C总线向EEPROM中写入数据,记录开机次数
没买板子之前,用protues画过电路图,实现了通过i2c总线向EEPROM中写入和读出数据. 今天,在自己买的板子上面写关于i2c总线的程序,有个地方忘了延时,调程序的时候很蛋疼.下面说说我对I2c ...
- 【转】从QDataStream向QByteArray中写入数据时的注意点(QT)
最近发现从QDataStream向QByteArray中写入数据常常是写不进去的,通过查看QT的源码: QDataStream &operator>>(QDataStream &a ...
- POI往word模板中写入数据
转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...
- Verilog利用$fdisplay命令往文件中写入数据
最近在做的事情是,用FPGA生成一些满足特定分布的序列.因此为了验证我生成的序列是否拥有预期的性质,我需要将生成的数据提取出来并且放到MATLAB中做数据分析. 但是网上的程序很乱,表示看不懂==其实 ...
- 复制excel表,往excel表中写入数据
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import jav ...
- POI向Excel中写入数据及追加数据
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...
- 计算机二级-C语言-程序填空题-190117记录-对文件的处理,复制两个文件,往新文件中写入数据。
//给定程序的功能是,调用函数fun将指定源文件中的内容赋值到指定目标文件中,复制成功时函数返回1,失败时返回0,把复制的内容输出到终端屏幕.主函数中源文件名放在变量sfname中,目标文件名放在变量 ...
- sql语句 怎么从一张表中查询数据插入到另一张表中?
sql语句 怎么从一张表中查询数据插入到另一张表中? ----原文地址:http://www.phpfans.net/ask/MTc0MTQ4Mw.html 比如我有两张表 table1 字段 un ...
随机推荐
- Search for a Range [LeetCode]
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Oracle后台进程
后台进程简介 启动例程时,Oracle不仅会分配SGA,还会启动后台进程:关闭例程时,Oracle不仅会释放SGA所占用的内存空间,而且还会释放后台进程所占用的Cpu和内存资源.Oracle提供了很多 ...
- 安装hadoop-2.3.0-cdh5.1.2全过程
工欲善其事,必先利其器,啥都不说,Hadoop下载:http://archive.cloudera.com/cdh5/cdh/5/ 选择好相应版本搞起,在本文讲述的是 围绕hadoop-2.3.0- ...
- 关于JDBC
脑补一下JDBC基础知识,原文链接:http://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html If you are ...
- ODBC 小例
#include "stdafx.h"#include <windows.h>#include <stdio.h>#include <iostream ...
- RAID在数据库存储上的应用-转
随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...
- HADOOP :: java.lang.ClassNotFoundException: WordCount
I am using eclipse to export the jar file of a map-reduce program. When i am run the jar using comma ...
- eclipse debug 过滤一些包
eclipse debug java程序的时候, 我们按F5的时候,常常会进入java自带类库里,这些类库并不是我们需要debug的代码,这样会影响debug的效率,我们可以在eclipse里设置,过 ...
- String类的写时拷贝
#include<iostream>using namespace std; class String;ostream& operator<<(ostream & ...
- 这是BUG吗?
百度首页的控制台里,有一段招聘信息,想必大家都知道吧,点击里面的链接地址跳到了百度招聘页面然后就发现了这个:,如果这不是BUG的话,那用户体验真是不好