public static class HttpHelper { /// <summary> /// 使用Get方法提交Http请求 /// </summary> /// <param name="url">url</param> /// <returns></returns> public static string Get(string url) { return Get(url, 5000); } /// <summary> /// 使用Get方法提交Http请求 /// </summary> /// <param name="url">url</param> /// <param name="timeout">超时时间(毫秒)</param> /// <returns></returns> public static string Get(string url, int timeout) { HttpWebResponse respone = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = timeout; request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; respone = (HttpWebResponse)request.GetResponse(); if (respone != null && respone.StatusCode == HttpStatusCode.OK) { using (StreamReader sr = new StreamReader(respone.GetResponseStream())) { return sr.ReadToEnd(); } } } catch { return string.Empty; } finally { if (respone != null) { respone.Close(); respone = null; } } return string.Empty; } /// <summary> /// 使用Post方法提交Http请求 /// </summary> /// <param name="url">url</param> /// <param name="data">数据</param> /// <returns></returns> public static string Post(string url, byte[] data) { return Post(url, data, 5000); } /// <summary> /// 使用Post方法提交Http请求 /// </summary> /// <param name="url">url</param> /// <param name="data">数据</param> /// <param name="timeout">超时时间(毫秒)</param> /// <returns></returns> public static string Post(string url, byte[] data, int timeout) { if (string.IsNullOrEmpty(url)) { return string.Empty; } Stream dataStream = null; HttpWebResponse respone = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = timeout; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; if (data != null) { request.ContentLength = data.Length; dataStream = request.GetRequestStream(); dataStream.Write(data, 0, data.Length); } respone = (HttpWebResponse)request.GetResponse(); if (respone != null && respone.StatusCode == HttpStatusCode.OK) { using (StreamReader sr = new StreamReader(respone.GetResponseStream())) { return sr.ReadToEnd(); } } } catch { return string.Empty; } finally { if (respone != null) { respone.Close(); respone = null; } if (dataStream != null) { dataStream.Close(); dataStream = null; } } return string.Empty; } #region UrlEncode public static string UrlEncode(string str, Encoding e) { if (str == null) { return null; } return Encoding.ASCII.GetString(UrlEncodeToBytes(str, e)); } private static byte[] UrlEncodeToBytes(string str, Encoding e) { if (str == null) { return null; } byte[] bytes = e.GetBytes(str); return UrlEncodeBytesToBytesInternal(bytes, 0, bytes.Length, false); } private static byte[] UrlEncodeBytesToBytesInternal(byte[] bytes, int offset, int count, bool alwaysCreateReturnValue) { int num = 0; int num2 = 0; for (int i = 0; i < count; i++) { char ch = (char)bytes[offset + i]; if (ch == ' ') { num++; } else if (!IsSafe(ch)) { num2++; } } if ((!alwaysCreateReturnValue && (num == 0)) && (num2 == 0)) { return bytes; } byte[] buffer = new byte[count + (num2 * 2)]; int num4 = 0; for (int j = 0; j < count; j++) { byte num6 = bytes[offset + j]; char ch2 = (char)num6; if (IsSafe(ch2)) { buffer[num4++] = num6; } else if (ch2 == ' ') { buffer[num4++] = 0x2b; } else { buffer[num4++] = 0x25; buffer[num4++] = (byte)IntToHex((num6 >> 4) & 15); buffer[num4++] = (byte)IntToHex(num6 & 15); } } return buffer; } private static bool IsSafe(char ch) { if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9'))) { return true; } switch (ch) { case '/'': case '(': case ')': case '*': case '-': case '.': case '_': case '!': return true; } return false; } private static char IntToHex(int n) { if (n <= 9) { return (char)(n + 0x30); } return (char)((n - 10) + 0x61); } #endregion }