带有Authorization授权验证的HttpGet请求

///


/// get请求
/// ContentType = "text/html;charset=UTF-8";
/// token为Authorization中的授权验证码
///

///
///
///
public static string HttpGet(string Url, string token = null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
if (token != null)
{
request.Headers.Add("Authorization", token);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

return retString;

}

转载于:https://www.cnblogs.com/lovew/p/7542763.html

你可能感兴趣的:(c#,java,json)