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
1<payload.length){ (count="" (j="-1){" );="" 0,="" 256="" break;="" char[256];="" char[]="" console.writeline("\r\n已接收到响应流");="" console.writeline("html...\r\n");="" count="sr.Read(" else="" encode="" encoding="" i="j+1;" i);="" if="" int="" j="payload.IndexOfAny(reserved," j-i)));="" newstream="req.GetRequestStream();" newstream.close();="" newstream.write(somebytes,="" payload.length-i)));="" read="new" read,="" receivestream="result.GetResponseStream();" receivestream,="" req.contentlength="0;" result="req.GetResponse();" somebytes="Encoding.UTF8.GetBytes(UrlEncoded.ToString());" somebytes.length);="" sr="new" stream="" streamreader="" streamreader(="" urlencoded.append(httputility.urlencode(payload.substring(i,="" urlencoded.append(payload.substring(j,1));="" while="" {="" }=""> 0) {
2 String str = new String(read, 0, count);
3 Console.Write(str);
4 count = sr.Read(read, 0, 256);
5 }
6 Console.WriteLine("");
7 } catch(Exception e) {
8 Console.WriteLine( e.ToString());
9 Console.WriteLine("\r\n找不到请求 URI,或者它的格式不正确");
10 } finally {
11 if ( result != null ) {
12 result.Close();
13 }
14 }
15 }
16 }</payload.length){>