[C #] testhttppost: a tool for testing the http post method

Source: Internet
Author: User
Tags response code

Author: zyl910.

In the past few days, I have debugged an interface using the HTTP POST protocol. I found several HTTP testing tools on the Internet, but they do not feel very useful. So I wrote a simple test tool in C.

I. Introduction

The default mode is "Post. Enter the URL address in the top text box, enter the post parameter in the "Post Data" text box, and then click "go" to send the request.
If you want to use the "get" mode. Click the "get" mode in the upper-left corner of the box, and then click "go" to send the request.
When the response content is garbled. Click the "response encoding" combo box and select the appropriate encoding. Click "go" to resend the request.

Ii. All code

Window code (frmtesthttppost. CS )--

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. text; using system. windows. forms; using system. io; using system. net; using system. net. cache; namespace testhttppost {public partial class frmtesthttppost: FORM {private encodinginfo [] _ encodings = NULL; // encoding set. private encoding _ resencoding = NULL; // response code. public frmtesthttp Post () {initializecomponent () ;}/// <summary> /// create an encoding object based on bodyname. /// </Summary> /// <Param name = "bodyname"> name of the current encoding used with the mail proxy body mark. </Param> // <returns> returns the encoding object. If no matching bodyname exists, null is returned. </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> // output the log text. /// </Summary> /// <Param name = "S"> log text </param> private void outlog (string s) {txtlog. appendtext (S + environment. newline); txtlog. scrolltocaret ();} private void outlog (string format, Params object [] ARGs) {outlog (string. format (format, argS);} private void frmtesthttppost_load (Object sender, eventargs e) {// HTTP Method cbomode. selectedindex = 1; // post // The response code 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 = "application/X-WWW-form-urlencoded"; httpwebrequest req; // log length if (txtlog. lines. length> 3000) txtlog. clear (); // = Main = outlog (string. format ("{2 }:{ 0} {1}", smode, Surl, datetime. now. tostring ("G"); try {// init Req = httpwebrequest. create (Surl) as httpwebrequest; req. method = smode; req. accept = "*/*"; req. keepalive = false; req. cachepolicy = new httprequestcachepolicy (httprequestcachelevel. nocachenostore); 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 ();} // response httpwebresponse res = req. getresponse () as httpwebresponse; try {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 # header: "); // header. for (INT I = 0; I <res. headers. count; ++ I) {outlog ("[{2}] {0 }:\ t {1}", Res. headers. keys [I], Res. headers [I], I) ;}// find the appropriate encoding = NULL; // encoding = encoding_frombodyname (res. characterset); // the character set and response of the subject are later found. characterset is different. // If (null = encoding) encoding = myencoding; encoding = _ resencoding; system. diagnostics. debug. writeline (encoding); // body outlog (". \ t # Body: "); // subject. using (Stream resstream = res. getresponsestream () {using (streamreader resstreamreader = new streamreader (resstream, encoding) {outlog (resstreamreader. readtoend () ;}} outlog (". \ t # OK. "); // succeeded .} finally {res. close () ;}} catch (exception ex) {outlog (ex. tostring ();} outlog (string. empty);} private void cboresencoding_selectedindexchanged (Object sender, eventargs e) {encodinginfo Ei = cboresencoding. selecteditem as encodinginfo; If (null = EI) return; _ resencoding = EI. getencoding ();}}}

 

 

Download source code --
Http://files.cnblogs.com/zyl910/TestHttpPost.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.