c#服务端获取API

public class Http
{
    #region Get请求
    ///    
    /// HTTP GET方式请求数据.     
    ///       
    /// URL.      
    ///      
    public string HttpGet(string url)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "GET";
        request.Accept = "*/*";
        request.Timeout = 15000;
        request.AllowAutoRedirect = false;

        WebResponse response = null;
        string responseStr = null;

        try
        {
            response = request.GetResponse();

            if (response != null)
            {
                StreamReader reader =new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                responseStr = reader.ReadToEnd();
                reader.Close();
            }
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            request = null;
            response = null;
        }

        return responseStr;
    }
    #endregion

    #region POST请求
    ///    
    /// HTTP POST方式请求数据     
    ///       
    /// URL.      
    ///      
    public string HttpPost(string url)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.Accept = "*/*";
        request.Timeout = 15000;
        request.AllowAutoRedirect = false;

        WebResponse response = null;
        string responseStr = null;

        try
        {
            response = request.GetResponse();
            if (response != null)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                responseStr = reader.ReadToEnd();
                reader.Close();
            }
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            request = null;
            response = null;
        }

        return responseStr;
    }
    ///     
    /// HTTP POST方式请求数据    
    ///     
    /// URL.    
    /// POST的数据    
    ///   
    public string HttpPost(string url, string Data)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        //request.ContentType = "application/json";
        request.Accept = "*/*";
        request.Timeout = 15000;
        request.AllowAutoRedirect = false;

        StreamWriter requestStream = null;
        WebResponse response = null;
        string responseStr = null;

        try
        {
            requestStream = new StreamWriter(request.GetRequestStream());
            requestStream.Write(Data);
            requestStream.Close();

            response = request.GetResponse();
            if (response != null)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                responseStr = reader.ReadToEnd();
               // File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("~//weixin/Weixin/") + @"\test.txt", responseStr);
                reader.Close();
            }
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            request = null;
            requestStream = null;
            response = null;
        }

        return responseStr;
    }
    #endregion

你可能感兴趣的:(c#服务端获取API)