模拟向服务器POST数据

原创 2006年06月02日 11:40:00

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

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

post方式模拟表单向服务器提交数据

post方式模拟表单提交数据

JS模拟APP客户端向服务器POST RSA加密的通信数据

这段时间在给公司开发B2B平台,其中用到RSA非对称加密来对APP和服务器通信进行数据加密 ,由于我们APP和服务端的程序员使用不同的语言,服务湍的PHP调试相当困难,所以琢磨了一个PHP页面来模拟A...

C# 模拟POST数据实例源码

  • 2013-08-04 12:27
  • 248KB
  • 下载

C# 模拟POST数据

  • 2013-07-09 15:11
  • 248KB
  • 下载

使用java模拟向web服务器发送get和post请求

package com.weixin.base.util; import java.io.BufferedReader; import java.io.IOException; import jav...

HttpUploadFile模拟前台POST上传图片和后台获取上传图片并上传至服务器

图片上传HttpUploadFile

get、post上传数据到服务器

  • 2015-12-27 00:02
  • 1.42MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)