原创:
http://www.imooc.com/article/8913
自己的例子:
web.config
<appSettings>
<add key="GmailUrl" value="https://aaa/bbb/ccc/"/>
</appSettings>
private string GetUserAddress(string id)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
string url = ConfigurationManager.AppSettings["GmailUrl"] + id;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
string retString = streamReader.ReadToEnd();
streamReader.Close();
stream.Close();
response.Close();
JArray value = (JArray)JsonConvert.DeserializeObject(retString);
return value[0]["Address"].ToString();
}
catch (Exception)
{
return "";
}
}
当然还需要Newtonsoft.Json.dll
此外, 如果是由https开头的,还需要加上ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;来建立安全连接。