首页 > Windows开发 > 详细

C# 小工具开发--DeBugHttp

时间:2016-11-21 19:59:29      阅读:151      评论:0      收藏:0      [点我收藏+]

    随着RESTful的流行,前后端分离,我们后端开发人员在开发过程中多http相关方法进行测试。这就是这个小工具的初衷。

   代码地址:https://github.com/gdoujkzz/DebugHttp  这是一个wpf小程序。

   

private string GetResponse (string method,string url,string param) {
           try
           {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Method = method;
                httpWebRequest.Timeout = 20000;
                if (method == "POST")
                {
                    byte[] btBodys = Encoding.UTF8.GetBytes(param);
                    httpWebRequest.ContentLength = btBodys.Length;
                    httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
                }
                //这里就是把httpwebrequest请求封装好了
                HttpWebResponse httpWebResonse = (HttpWebResponse) httpWebRequest.GetResponse();
                string responseContent = null;
                using (StreamReader streamReader = new StreamReader(httpWebResonse.GetResponseStream()))
                {
                    //流文件的读取。
                    responseContent = streamReader.ReadToEnd();
                }
                httpWebRequest.Abort();
                httpWebResonse.Close();
                return responseContent;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }    

 

C# 小工具开发--DeBugHttp

原文:http://www.cnblogs.com/gdouzz/p/6086141.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!