• 利用WebClient 制作调试Http的post 和 get 工具


    有时候需要看网页的提交效果,但需要改变一些自己的参数,于是想到了做个这样的工具



     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Collections.Specialized;
    using System.Text.RegularExpressions;

    namespace HTTPGetPost
    {
        
    public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
            }

            
    private void btnGo_Click(object sender, EventArgs e)
            {


                
    if (rdbGet.Checked)
                {
                    txtRe.Text 
    = GetData(txtUrl.Text, txtHead.Text);
                }
                
    else
                {

                    txtRe.Text 
    = GetData(txtUrl.Text, txtHead.Text,txtPost.Text );
                }
                

            }
            
    private string GetData(string url, string head,string post)
            {
                WebClient wc 
    = AddHead(txtHead.Text);
                
    byte[] re = wc.UploadValues(url, "POST", PostData(post));
                
    return EncodeHtml(re);
            }

            
    private string  GetData(string url, string head)
            {
                WebClient wc 
    = AddHead(txtHead.Text);

                
    byte[] re = wc.DownloadData(url);
                
                
    return EncodeHtml(re);
            }
            
    private WebClient AddHead(string head)
            {
                WebClient wc 
    = new WebClient();
                
    string[] p = head.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                
    for (int i = 0; i < p.Length; i++)
                {
                    
    int k = p[i].IndexOf("\t");
                    
                    wc.Headers.Add (p[i].Substring(
    0, k), p[i].Substring(k + 1, p[i].Length - k - 1));
                }
                wc.Credentials 
    = CredentialCache.DefaultCredentials;
                
    return wc;

            }
            
    private NameValueCollection PostData(string poststr)
            {
                NameValueCollection PostVars 
    = new NameValueCollection();
                
    string[] p=poststr.Split (new string[]{"\r\n"},StringSplitOptions.RemoveEmptyEntries);

                
    for (int i = 0; i < p.Length; i++)
                { 
                    
    int k=p[i].IndexOf ("\t");
                    PostVars.Add (p[i].Substring (
    0,k),p[i].Substring (k+1,p[i].Length --1));
                }
                
    return PostVars;         

            }
            
    private string EncodeHtml(byte[] myDataBuffer)
            {
                
    string strWebData = Encoding.UTF8.GetString(myDataBuffer);
                Match charSetMatch 
    = Regex.Match(strWebData, "(?<=<meta.*charset=).*?(?=\")", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                if (charSetMatch.Value == "")
                {
                    charSetMatch 
    = Regex.Match(strWebData, "(?<=<meta.*charset=).*?(?=;)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                }
                
    if (charSetMatch.Value == "")
                    
    return strWebData;
                
    else
                {
                    
    return System.Text.Encoding.GetEncoding(charSetMatch.Value).GetString(myDataBuffer);
                }
           }

        }
    }

  • 相关阅读:
    bzoj2287 [POJ Challenge]消失之物
    bzoj4504 K个串
    Tjoi2016&Heoi2016 字符串
    bzoj2555 SubString
    WC2017 游记
    《大灌篮》观后感
    Codeforces Round #394 (Div. 2)
    bzoj3489 A simple rmq problem
    Goodbye Bingshen
    COGS2216 你猜是不是KMP
  • 原文地址:https://www.cnblogs.com/szyicol/p/1818045.html
Copyright © 2020-2023  润新知