public string SendGetRequest(string baseurl, string parameters) { string parassb = parameters; if (parassb.Length > 0) { baseurl += "?" + parassb; } HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl); req.Method = "GET"; req.ContinueTimeout = 1000; string result = String.Empty; using (StreamReader reader = new StreamReader(req.GetResponseAsync().Result.GetResponseStream())) { result = reader.ReadToEnd(); } return result; }
上面代码是GET方式请求;
public async Task<string> PostRequest(string url, string parameters) { var request = HttpWebRequest.Create(url); request.Method = "POST"; using (Stream result = await request.GetRequestStreamAsync()) { byte[] bytes = Encoding.UTF8.GetBytes(parameters); result.Write(bytes, 0, bytes.Length); } var response = await request.GetResponseAsync(); using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { string content = reader.ReadToEnd(); return content; } } return null; }
这是POST请求方式