企业号微信开发中使用到的get和post 方法

跨域名访问get方法
///
/// 创建GET方式的HTTP请求
///
/// 请求的URL
/// 请求的超时时间
///
public static JObject GetHttpResponse(string url, int? timeout)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException(“url”);
}
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = “GET”;
request.UserAgent = DefaultUserAgent;

        if (timeout.HasValue)
        {
            request.Timeout = timeout.Value;
        }
        HttpWebResponse httpweb = request.GetResponse() as HttpWebResponse;
        StreamReader reader = new StreamReader(httpweb.GetResponseStream());
        string returnStr = "";
        returnStr = reader.ReadToEnd();
        JObject result = (JObject)JsonConvert.DeserializeObject(returnStr);
        return result;
    }

跨域名访问Post方法。
///
/// 跨域访问
///
/// url
/// 参数
///
public static JObject HttpPostJson(string url, string param, int time = 60000)
{
Uri address = new Uri(url);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = “POST”;
request.ContentType = “application/json;charset=utf-8”; //”application/x-www-form-urlencoded”;
request.Timeout = time;
byte[] byteData = UTF8Encoding.UTF8.GetBytes(param == null ? “” : param);
request.ContentLength = byteData.Length;
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
string result = “”;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
}
JObject ol = (JObject)JsonConvert.DeserializeObject(result);
return (ol);
}

你可能感兴趣的:(微信)