<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      zyl910

      優化技巧、硬件體系、圖像處理、圖形學、游戲編程、國際化與文本信息處理。

        博客園 :: 首頁 :: 博問 :: 閃存 :: 新隨筆 :: 聯系 :: 訂閱 訂閱 :: 管理 ::

      作者: zyl910

        最近在調試Http POST協議的接口時,對 TestHttpPost 進行了改進,增加了增加輸出文件、設置請求頭的功能。故發布了發布v1.2版。

      一、變更說明

      v1.2的變更說明——

      • 框架版本改為 .NET Framework 4.5. 因為 WIndows 7、Windows Server 2008等操作系統自帶了.NET Framework 4.5或更高版本, 節省了安裝 .NET 運行時 的工作.
      • 增加輸出文件的功能. 增加了“OutputFile”的復選框及文本框.
      • 增加設置請求頭的功能. 增加了“Header”選項卡, 其中可以設置請求頭。每一行的格式為“key: value”.

      是在 hezlog 的改進版的基礎上改造的。感謝 hezlog 的貢獻.

      二、使用介紹

      默認是“POST”模式。在最上面的文本框中輸入Url地址,然后在“Post Data”文本框中輸入Post參數,再點擊“Go”按鈕發送請求。
      如果想使用“GET”模式。便點擊左上角的組合框,選擇“GET”模式,再點擊“Go”按鈕發送請求。

      下面介紹了更多用法。

      • 當發現回應內容亂碼時。點擊“Response Encoding”組合框,選擇合適的編碼。再點擊“Go”按鈕發送請求。
      • 當需要查看詳細日志時。勾選“log header”復選框。再點擊“Go”按鈕發送請求。
      • 當需要輸出文件時。勾選“Output file”復選框,隨后在旁邊的文本框里輸入“輸出文件名”。再點擊“Go”按鈕發送請求。
      • 當需要設置請求頭時。點擊“Header”切換到該選項卡,隨后在其中的文本框里輸入請求頭的內容,例如“Cookie: abc”。再點擊“Go”按鈕發送請求。

      TestHttpPost_v1_2

      三、全部代碼

      窗口的代碼(FrmTestHttpPost.cs)——

      ////////////////////////////////////////////////////////////
      // FrmTestHttpPost.cs: 測試Http的POST方法.
      // Author: zyl910
      // Blog: http://www.rzrgm.cn/zyl910
      // URL: http://www.rzrgm.cn/zyl910/archive/2012/09/19/TestHttpPost.html
      // Version: V1.20
      // Updata: 2025-06-21
      //
      ////////////////////////////////////////////////////////////
      
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.IO;
      using System.IO.Pipes;
      using System.Net;
      using System.Net.Cache;
      using System.Text;
      using System.Threading;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      
      namespace TestHttpPost
      {
          public partial class FrmTestHttpPost : Form
          {
              private EncodingInfo[] _Encodings = null;   // 編碼集合.
              private Encoding _ResEncoding = null;   // 回應的編碼.
              private bool _OutputFileAllow = false; // 允許輸出文件.
              private string _OutputFilePath = null; // 輸出文件的路徑.
              private string[] _HeaderLines = null; // 請求頭的行列表.
      
              public FrmTestHttpPost()
              {
                  InitializeComponent();
              }
      
              /// <summary>
              /// 根據BodyName創建Encoding對象。
              /// </summary>
              /// <param name="bodyname">與郵件代理正文標記一起使用的當前編碼的名稱。</param>
              /// <returns>返回Encoding對象。若沒有匹配的BodyName,便返回null。</returns>
              public static Encoding Encoding_FromBodyName(string bodyname)
              {
                  if (string.IsNullOrEmpty(bodyname)) return null;
                  try
                  {
                      foreach (EncodingInfo ei in Encoding.GetEncodings())
                      {
                          Encoding e = ei.GetEncoding();
                          if (0 == string.Compare(bodyname, e.BodyName, true))
                          {
                              return e;
                          }
                      }
                  }
                  catch
                  {
                  }
                  return null;
              }
      
              /// <summary>
              /// 輸出日志文本.
              /// </summary>
              /// <param name="s">日志文本</param>
              private void OutLog(string s)
              {
                  txtLog.Invoke(new Action<string>(p =>
                  {
                      txtLog.AppendText(p + Environment.NewLine);
                      txtLog.ScrollToCaret();
                  }), s);
              }
      
              private void OutLog(string format, params object[] args)
              {
                  OutLog(string.Format(format, args));
              }
      
              private void HttpReq(Encoding myEncoding, string sMode, string sUrl, string sContentType, string sPostData, bool bVerbose)
              {
                  HttpWebRequest req;
      
                  // == main ==
                  if (bVerbose) OutLog(string.Format("{3}: {0} {1} {2}", sMode, sUrl, sPostData, DateTime.Now.ToString("g")));
                  try
                  {
                      bool showRequestHeader = false;
                      // init
                      req = HttpWebRequest.Create(sMode == "GET" ? sUrl + sPostData : sUrl) as HttpWebRequest;
                      req.Method = sMode;
                      req.Accept = "*/*";
                      req.KeepAlive = false;
                      req.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
                      string[] headerLines = _HeaderLines;
                      if (null != headerLines && headerLines.Length > 0) {
                          var headers = req.Headers;
                          // Set.
                          showRequestHeader = true;
                          foreach(string line in headerLines) {
                              if (string.IsNullOrEmpty(line)) continue;
                              int pos = line.IndexOf(':');
                              if (pos <= 0) continue;
                              string key = line.Substring(0, pos).Trim();
                              string value = line.Substring(pos + 1).Trim();
                              if (string.IsNullOrEmpty(key)) continue;
                              headers[key] = value;
                          }
                      }
                      if (0 == string.Compare("POST", sMode))
                      {
                          byte[] bufPost = myEncoding.GetBytes(sPostData);
                          req.ContentType = sContentType;
                          req.ContentLength = bufPost.Length;
                          Stream newStream = req.GetRequestStream();
                          newStream.Write(bufPost, 0, bufPost.Length);
                          newStream.Close();
                      }
                      if (showRequestHeader) {
                          var headers = req.Headers;
                          // Output RequestHeader
                          OutLog(".\t#RequestHeader:");
                          for (int i = 0; i < headers.Count; ++i) {
                              OutLog("[{2}] {0}:\t{1}", headers.Keys[i], headers[i], i);
                          }
                      }
      
                      // Response
                      HttpWebResponse res = req.GetResponse() as HttpWebResponse;
                      try
                      {
                          if (bVerbose)
                          {
                              OutLog("Response.ContentLength:\t{0}", res.ContentLength);
                              OutLog("Response.ContentType:\t{0}", res.ContentType);
                              OutLog("Response.CharacterSet:\t{0}", res.CharacterSet);
                              OutLog("Response.ContentEncoding:\t{0}", res.ContentEncoding);
                              OutLog("Response.IsFromCache:\t{0}", res.IsFromCache);
                              OutLog("Response.IsMutuallyAuthenticated:\t{0}", res.IsMutuallyAuthenticated);
                              OutLog("Response.LastModified:\t{0}", res.LastModified);
                              OutLog("Response.Method:\t{0}", res.Method);
                              OutLog("Response.ProtocolVersion:\t{0}", res.ProtocolVersion);
                              OutLog("Response.ResponseUri:\t{0}", res.ResponseUri);
                              OutLog("Response.Server:\t{0}", res.Server);
                              OutLog("Response.StatusCode:\t{0}\t# {1}", res.StatusCode, (int)res.StatusCode);
                              OutLog("Response.StatusDescription:\t{0}", res.StatusDescription);
      
                              // header
                              OutLog(".\t#ResponseHeader:");  // 頭.
                              for (int i = 0; i < res.Headers.Count; ++i)
                              {
                                  OutLog("[{2}] {0}:\t{1}", res.Headers.Keys[i], res.Headers[i], i);
                              }
                          }
      
                          // 找到合適的編碼
                          Encoding encoding = null;
                          //encoding = Encoding_FromBodyName(res.CharacterSet);	// 后來發現主體部分的字符集與Response.CharacterSet不同.
                          //if (null == encoding) encoding = myEncoding;
                          encoding = _ResEncoding;
                          System.Diagnostics.Debug.WriteLine(encoding);
      
                          // body
                          if (bVerbose) OutLog(".\t#Body:");    // 主體.
                          using (Stream resStreamRaw = res.GetResponseStream())
                          {
                              Stream resStream = resStreamRaw;
                              if (_OutputFileAllow) {
                                  MemoryStream memoryStream = new MemoryStream();
                                  resStreamRaw.CopyTo(memoryStream);
                                  OutLog(string.Format("Byte size: {0} // 0x{0:X}", memoryStream.Length));
                                  if (!string.IsNullOrEmpty(_OutputFilePath)) {
                                      string fullPath = Path.GetFullPath(_OutputFilePath);
                                      memoryStream.Position = 0;
                                      try {
                                          File.WriteAllBytes(fullPath, memoryStream.GetBuffer());
                                          OutLog(string.Format("Output file OK. {0}", fullPath));
                                      } catch (Exception ex1) {
                                          OutLog(string.Format("Output file fail! {0}", fullPath));
                                          OutLog(ex1.ToString());
                                      }
                                  }
                                  // next.
                                  memoryStream.Position = 0;
                                  resStream = memoryStream;
                              }
                              using (StreamReader resStreamReader = new StreamReader(resStream, encoding))
                              {
                                  OutLog(resStreamReader.ReadToEnd());
                              }
                          }
                          if (bVerbose) OutLog(".\t#OK.");  // 成功.
                      }
                      finally
                      {
                          res.Close();
                      }
                  }
                  catch (Exception ex)
                  {
                      OutLog(ex.ToString());
                  }
                  OutLog(string.Empty);
      
              }
      
              private void FrmTestHttpPost_Load(object sender, EventArgs e)
              {
                  // Http方法
                  cboMode.SelectedIndex = 1;  // POST
                  cboContentType.SelectedIndex = 0; // json
      
                  // 回應的編碼
                  cboResEncoding.Items.Clear();
                  _Encodings = Encoding.GetEncodings();
                  cboResEncoding.DataSource = _Encodings;
                  cboResEncoding.DisplayMember = "DisplayName";
                  _ResEncoding = Encoding.UTF8;
                  cboResEncoding.SelectedIndex = cboResEncoding.FindStringExact(_ResEncoding.EncodingName);
      
              }
      
              private void btnGo_Click(object sender, EventArgs e)
              {
                  Encoding myEncoding = Encoding.UTF8;
                  string sMode = (string)cboMode.SelectedItem;
                  string sUrl = txtUrl.Text;
                  string sPostData = txtPostData.Text;
                  string sContentType = cboContentType.SelectedItem.ToString(); // "application/x-www-form-urlencoded";
                  TextReader read = new System.IO.StringReader(sPostData);
                  _OutputFileAllow = chkOutputFile.Checked;
                  _OutputFilePath = txtOutputFile.Text.Trim();
                  if (_OutputFileAllow && string.IsNullOrEmpty(_OutputFilePath)) {
                      MessageBox.Show("The OutputFile textbox is empty!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                      return;
                  }
                  _HeaderLines = null;
                  if (chkHeader.Checked) {
                      _HeaderLines = txtHeader.Lines;
                  }
      
                  // Log Length
                  //if (txtLog.Lines.Length > 3000) txtLog.Clear();
      
                  if (checkBox1.Checked)
                  {
                      Task.Run(() =>
                      {
                          do
                          {
                              if (string.IsNullOrWhiteSpace(sUrl))
                                  break;
                              sPostData = read.ReadLine();
                              if (string.IsNullOrWhiteSpace(sPostData))
                                  break;
                              // == main ==
                              HttpReq(myEncoding, sMode, sUrl, sContentType, sPostData, checkBox2.Checked);
      
                              Thread.Sleep((int)numericUpDown1.Value);
      
                          } while (!string.IsNullOrWhiteSpace(sPostData));
                      });
                  }
                  else
                  {
                      // == main ==
                      HttpReq(myEncoding, sMode, sUrl, sContentType, sPostData, checkBox2.Checked);
                  }
      
              }
      
              private void cboResEncoding_SelectedIndexChanged(object sender, EventArgs e)
              {
                  EncodingInfo ei = cboResEncoding.SelectedItem as EncodingInfo;
                  if (null == ei) return;
                  _ResEncoding = ei.GetEncoding();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  txtLog.Clear();
              }
          }
      }
      

      源碼: https://github.com/zyl910/TestHttpPost
      程序下載: https://github.com/zyl910/TestHttpPost/releases/tag/v1.2

      參考文獻

      posted on 2025-06-21 20:48  zyl910  閱讀(125)  評論(3)    收藏  舉報
      主站蜘蛛池模板: 亚洲精品乱码久久观看网| 一区二区三区四区五区自拍| 日韩永久永久永久黄色大片| 国产精品老年自拍视频| 在线中文字幕国产一区| 日韩欧美一卡2卡3卡4卡无卡免费2020| 国产v综合v亚洲欧美大天堂| 成人嫩草研究院久久久精品| 又大又紧又粉嫩18p少妇| 久久综合亚洲鲁鲁九月天| 少妇人妻av毛片在线看| 人人妻人人澡人人爽不卡视频| 美女裸体黄网站18禁止免费下载| ww污污污网站在线看com| 芳草地社区在线视频| 国产网友愉拍精品视频手机| 亚洲午夜av一区二区| 国产精品一区二区三区黄| 国产精品亚洲二区在线看| 午夜天堂精品久久久久| 天天躁夜夜躁天干天干2020| 起碰免费公开97在线视频| 亚洲啪啪精品一区二区的| 亚洲精品色国语对白在线| 少妇办公室好紧好爽再浪一点| 欧美牲交a欧美牲交aⅴ图片| 亚洲大尺度无码无码专线| 国产一区二区精品自拍| 成年女人免费碰碰视频| 亚洲av本道一区二区| 中文字幕久久国产精品| 亚洲一区二区三区在线观看精品中文| 久久精品色一情一乱一伦| 亚洲一区二区三区久久综合| 国产成人午夜福利精品| 午夜av福利一区二区三区| 国产高跟黑色丝袜在线| av中文无码乱人伦在线观看| 日韩精品亚洲精品第一页| 日韩亚洲视频一区二区三区| 成人片在线看无码不卡|