C# 小工具开发--DeBugHttp

    随着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;
            }    

 

posted @ 2016-11-21 17:13  GDOUJKZZ  阅读(691)  评论(0编辑  收藏  举报