国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在.NET 中模擬提交Post數(shù)據(jù)

2024-07-10 12:59:17
字體:
供稿:網(wǎng)友
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("按任意鍵繼續(xù)...");
console.readline();

return;
}

public static void showusage() {
console.writeline("嘗試發(fā)送 (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已接收到響應(yīng)流");
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找不到請(qǐng)求 uri,或者它的格式不正確");
} finally {
if ( result != null ) {
result.close();
}
}
}
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 云霄县| 高邮市| 富平县| 台东县| 霍邱县| 宁河县| 前郭尔| 武隆县| 延边| 石嘴山市| 湘潭县| 洱源县| 星子县| 肥乡县| 福建省| 彭水| 吴江市| 盐池县| 梅州市| 秦安县| 历史| 二连浩特市| 开原市| 洛宁县| 六安市| 道真| 金昌市| 黔西| 微博| 合肥市| 康乐县| 建德市| 白水县| 海原县| 武乡县| 长治市| 海城市| 伊吾县| 新乐市| 南阳市| 漠河县|