C# 后台POST和GET 获取数据

 C# 后台POST和GET 获取数据

private string PostData(string url, string postData)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] data = encoding.GetBytes(postData);
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);

    myRequest.Method = "POST";
    myRequest.ContentType = "application/x-www-form-urlencoded";
    myRequest.ContentLength = data.Length;
    Stream newStream = myRequest.GetRequestStream();

    newStream.Write(data, 0, data.Length);
    newStream.Close();

    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
    string content = reader.ReadToEnd();
    reader.Close();
    return content;
}

private string GetData(string url)
{
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
    myRequest.Method = "GET";
    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
    string content = reader.ReadToEnd();
    reader.Close();
    return content;
}
原文地址:https://www.cnblogs.com/wolfocme110/p/3975270.html