zoukankan      html  css  js  c++  java
  • c#取得post和get的数据和模拟发送

    可以放在控制器的入口函数中,这样载入就会被调用

    asp.net mvc,get+post:

    public ActionResult Index()
            {
                if (Request.QueryString.Count > 0)
                {
                    foreach (var item in Request.QueryString)
                    {
                        Console.WriteLine(item);
                    }
                }
    
                if (Request.Form.Count > 0)
                {
                    foreach (var item in Request.Form)
                    {
                        Console.WriteLine(item);
                    }
                }
    
                return null;
            }
    View Code

    模拟发送post:

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var targetWeb = "http://localhost:60797/";
                var reqContent = "Test1=123&Test2=345";
    
                var data = Encoding.UTF8.GetBytes(reqContent);
    
                var req = (HttpWebRequest)WebRequest.Create(targetWeb);
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = data.Length;
                req.Method = "POST";
                var stream = req.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                var resp = req.GetResponse();
                stream = resp.GetResponseStream();
    
                var reader = new StreamReader(stream);
                string response = reader.ReadToEnd();
                req.Abort();
                resp.Close();
    
                Console.WriteLine("发出请求: " + reqContent + " 目标地址: " + targetWeb);
                Console.WriteLine("返回响应: " + response);
                Console.Read();
            }
        }
    }
    View Code

    模拟发送get:

    using System;
    using System.IO;
    using System.Net;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var targetWeb = "http://localhost:60797?Test1=123&Test2=345";
    
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(targetWeb);
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "GET";
                WebResponse resp = req.GetResponse();
                var stream = resp.GetResponseStream();
    
                StreamReader reader = new StreamReader(stream);
                string response = reader.ReadToEnd();
                req.Abort();
                resp.Close();
    
                Console.WriteLine("目标地址: " + targetWeb);
                Console.WriteLine("返回响应: " + response);
                Console.Read();
            }
        }
    }
    View Code
  • 相关阅读:
    SQL Server 中的事务与事务隔离级别以及如何理解脏读, 未提交读,不可重复读和幻读产生的过程和原因
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSAS 系列
    微软BI 之SSRS 系列
    微软BI 之SSRS 系列
    配置 SQL Server Email 发送以及 Job 的 Notification通知功能
  • 原文地址:https://www.cnblogs.com/hont/p/4249963.html
Copyright © 2011-2022 走看看