快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

在.NET 中模仿提交Post数据

using System;
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();
}
}
}
}





返回类别: 教程
上一教程: ASP.NET强盛的性能(一)
下一教程: .Net开发平台研究(三)

您可以阅读与"在.NET 中模仿提交Post数据"相关的教程:
· 使用perl.net访问数据库
· .NET 数据访问架构指南(二)
· .net中数据库事务机制
· ASP.NET中数据库的操作初步(2)
· .NET 数据访问架构指南(一)
    微笑服务 优质保证 索取样品