代码地址: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; }
,