1.GET请求:
public static string Get(string url) { string buffer = ""; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Proxy = new WebProxy(); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8); buffer = reader.ReadToEnd(); reader.Close(); response.Close(); } catch (Exception ex) { MessageBox.Show("网络异常"); return ""; } return buffer; }
2.POST请求:
public static string Send(string url, byte[] data) { string buffer = ""; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Proxy = new WebProxy(); request.Timeout = 1000 * 600; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; Stream newStream = request.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8); buffer = reader.ReadToEnd(); reader.Close(); response.Close(); } catch (Exception ex) { MessageBox.Show("网络异常"); return ""; } return buffer; }