.net 后台发送http请求
需求是通过后台代码,发送http请求
//创建http的WebRequest
WebRequest request = WebRequest.Create("http://api.sms.ronghub.com/sendCode.json");
request.Method = "POST";
string postData = "mobile="+ mobile + "&templateId="+ Constant.TEMPLATE_REGISTER + "®ion=86";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
//这里增加需要的参数放在请求头里
request.Headers.Add("App-Key", Constant.APP_KEY);
request.Headers.Add("Nonce", Random);
request.Headers.Add("Timestamp", Timestamp);
request.Headers.Add("Signature", Signature);
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//获取返回的response
WebResponse resp = request.GetResponse();
dataStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
//返回的结果转换成实体类
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
ReturnJson _ReturnJson = jsonSerializer.Deserialize<ReturnJson>(responseFromServer);
reader.Close();
dataStream.Close();
resp.Close();