httpget & httppost

internal static string httpGet(string url)
{
    string ret = null;
    HttpWebRequest webRequest = null;
    try
    {
        webRequest = WebRequest.Create(url) as HttpWebRequest;
        webRequest.Method = "GET";
        webRequest.ContentType = "application/json; encoding=utf-8";
        HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
        if (webResponse != null)
        {
            using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
            {
                string content = sr.ReadToEnd();
                ret = content;
            }
            webResponse.Close();
        }
    }
    catch (WebException webException)
    {
        Console.WriteLine("*** WebException ***");
        Console.WriteLine(webException.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("*** Exception ***");
        Console.WriteLine(ex.Message);
    }
    finally
    {
        if (webRequest != null)
        {
            webRequest.Abort();
        }
    }
    return ret;
}

参考文献:
https://www.cnblogs.com/SavionZhang/p/11392709.html

标签: none

添加新评论