Simulate submission of POST data in .NET

zhaozj2021-02-08  311

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 ("Press anyg" to continue ... ");

Console.readline ();

Return;

}

Public static void showusage () {

Console.writeline ("Try to send (POST) to URL");

Console.writeLine ();

Console.writeLine ("Usage ::");

Console.writeLine ("ClientPost Url [PostData]);

Console.writeLine ();

Console.writeLine ("Example ::");

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

j = payload.indexofany (reserved, i);

IF (j == - 1) {

UrlencoDed.Append (httputility.urlencode));

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 ();

Street.getReStream = Result.getResponseSstream ();

Encoding encode = system.text.encoding.getencoding ("UTF-8");

StreamReader SR = New StreAMReader (ReceiveStream, Encode);

Console.writeline ("/ R / N has received response stream");

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 does not find the request URI, or its format is incorrect");

} finally {

IF (result! = null) {

Result.close ();

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-357.html

New Post(0)