- 热门文章:
- · 以武學的視角來戲說.NET程序員的倚天之術
- · XP方法学习总结及对小组开发的思考
- · 程序员的.NET时代(二)
- · 程序员的.NET时代(一)
- · 品味.NET巨著——书评《Microsoft .NET框架程序设计(修订版)》
- · 解决.NET(WebApplication)安装部署的不能选择安装路径的问题
- · Beta 1 到 Beta 2 改变详细列表(英文)Beta 1 to Beta 2 Changes
- · Microsoft Visual J#.NET (JSharp) Version 7.0 Beta 1 out
- · Microsoft Visual Studio.NET及Borland Delphi6初探
- · .NET对软件安装的冲击
- · Microsoft Tech Ed
- · .NET 和 COM 之间的相互访问
上一篇:.net下分层架构系统的开发技术规范(2) >>
在.NET 中模拟提交Post数据
using System.Net;
using System.IO;
using System.Text;
using System.Web;
class ClientPOST {
public static void Main(string[] args) {
if (args.Length < 1) {
showusage();
} else {
if (args.Length < 2 ) {
getPage(args[0], "s1=foods2=bart(&S)");
} else {
getPage(args[0], args[1]);
}
}
Console.WriteLine();
Console.WriteLine("按任意键继续...");
Console.ReadLine();
return;
}
public static void showusage() {
Console.WriteLine("尝试发送 (POST) 到 URL 中");
Console.WriteLine();
Console.WriteLine("用法::");
Console.WriteLine("ClientPOST URL [postdata]");
Console.WriteLine();
Console.WriteLine("示例::");
Console.WriteLine("ClientPOST http://www.microsoft.com s1=food&s2=bart");
}
public static void getPage(String url, String payload) {
WebResponse result = null;
try {
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {@#?@#, @#=@#, @#&@#};
byte[] SomeBytes = null;
if (payload != null) {
int i=0, j;
while(i<payload.Length){
j=payload.IndexOfAny(reserved, i);
if (j==-1){
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j-i)));
UrlEncoded.Append(payload.Substring(j,1));
i = j+1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
} else {
req.ContentLength = 0;
}
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\r\n已接收到响应流");
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0) {
String str = new String(read, 0, count);
Console.Write(str);
count = sr.Read(read, 0, 256);
}
Console.WriteLine("");
} catch(Exception e) {
Console.WriteLine( e.ToString());
Console.WriteLine("\r\n找不到请求 URI,或者它的格式不正确");
} finally {
if ( result != null ) {
result.Close();
}
}
}
}
下一篇:以武學的視角來戲說.NET程序員的倚天之術 >>
相关文章:
- · 介绍.NET中的委派(一)
- · .NET架构的核心开发技术
- · 控制VC++.NET中WEB对话框的HTML元素属性
- · Borland Eyeing the Chasm Between Java and .NET
- · .Net 是未来的趋势, 为什么?
- · Microsoft .NET Development Platform的Linux版本(Mono)出现
- · 价值上千美元的Visual Studio.NET β2的培训
- · 使用 Microsoft.NET Frameworks 创建Windows应用程序
- · DataGrid连接Access的快速分页法(1)——需求与现状
- · 用Socket类构建网页下载器
- · COM与.NET的互操作(初级)
- · .NET 的对象关系持久化机制(1)
- · Websharp使用说明(8)
- · Websharp使用说明(7)
- · Websharp使用说明(6)
- · Websharp使用说明(5)
- · Websharp使用说明(4)
- · Websharp使用说明(3)
- · Websharp使用说明(2)
- · Websharp使用说明(1)
- · 使.NET命名空间符合标准
- · Repeater控件的分页问题
- · 用.Net构架你的系统(基类的搭建思路)
- · .NET断想
- · 常见 Datagrid 错误
- · .NET 数据访问架构指南(二)
- · .NET 数据访问架构指南(一)
- · 开发合作 Microsoft .NET 解决方案
- · Microsoft .NET 框架常见问题
- · Effective C#-Working with Strings
- · 使用WMI获得硬盘的信息
- · MSBuild入门
- · ADO.NET对象的构造(7)_OleDbParameter(中)
- · ADO.NET对象的构造(7)_OleDbParameter(上)
- · ADO.NET对象的构造(4)_DataColumn(续)
- · ADO.NET对象的构造(3)_DataTable(续)
- · .NET开发平台研究(四)
- · .Net开发平台研究(三)
