zoukankan      html  css  js  c++  java
  • 利用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);
                }
           }

        }
    }

  • 相关阅读:
    eclipse config 2 tab -&gt; space
    APUE读书笔记-第13章-守护进程
    【转】Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
    【转】Android学习基础自定义Checkbox组件
    【转】declare-styleable的使用(自定义控件) 以及declare-styleable中format详解
    【转】android 自定义控件 使用declare-styleable进行配置属性(源码角度)
    【转】Android SwitchButton(滑动开关)
    【转】Android 如何在Eclipse中查看Android API源码 及 support包源码
    【转】如何在eclipse里关联查看android源码
    【转】setTag()/getTag()
  • 原文地址:https://www.cnblogs.com/szyicol/p/1818045.html
Copyright © 2011-2022 走看看