在.NET 中模拟提交Post数据

ASP.NET 专栏收录该内容
3 篇文章 0 订阅

 

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();
            }
        }
    }
}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页

打赏

foxfly

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值