标签:
public static String RequestUrl(String url, byte[] data, String method = "POST") { try { WebRequest request = WebRequest.Create(url); request.Method = method; request.ContentType = "application/x-www-form-urlencoded"; if (data != null && data.Length > 0) { request.ContentLength = data.Length; Stream newStream = request.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); } else { request.ContentLength = 0; } WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); MemoryStream ms = new MemoryStream(); long ChunkSize = 1024; byte[] buffer = new byte[ChunkSize]; long dataLengthToRead = response.ContentLength;//获取响应数据的总大小 while (dataLengthToRead > 0) { int lengthRead = stream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 ms.Write(buffer, 0, lengthRead); dataLengthToRead = dataLengthToRead - lengthRead; } stream.Close(); response.Close(); string responseText = Encoding.UTF8.GetString(ms.ToArray()); return responseText; } catch (Exception ex) { return ex.Message; } }
标签:
原文地址:http://www.cnblogs.com/slu182/p/4250377.html