如何在后台调用接口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在后台调用接口相关的知识,希望对你有一定的参考价值。
string url = "http://localhost:12083/api/common/GetVerifyCode"; string strType = "application/x-www-form-urlencoded"; string strPar = "userName=18382565651";//拼接参数 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);//创建请求地址 httpWebRequest.ContentType = strType;//设置http标头 httpWebRequest.Method = "POST";//设置提交方式 httpWebRequest.Timeout = 600000;//设置过期时间 byte[] btBodys = Encoding.UTF8.GetBytes(strPar);//重写字符串为字节码 httpWebRequest.ContentLength = btBodys.Length; httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//接收返回内容 StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());//读写为流 string responseContent = streamReader.ReadToEnd();//返回的数据 httpWebResponse.Close(); streamReader.Close(); httpWebRequest.Abort(); httpWebResponse.Close();
以上是关于如何在后台调用接口的主要内容,如果未能解决你的问题,请参考以下文章