C# 中使用POST调用接口的几种传参方式

说明:第一、二种的例子是直接调用接口,接口的说明如下:

C# 中使用POST调用接口的几种传参方式_第1张图片

C# 中使用POST调用接口的几种传参方式_第2张图片

第3、4种的例子是调用别人的接口,然后写成自己的webapi(.asmx页面)。

1.第一种:参数是以url的形式追加到地址后面


string posturl="http://api.daiyicloud.com/asmx/smsservice.aspx?name=n01&pwd=B16F57451A&content=您好,您当前等待办理的1651还有1位,请留意叫号信息等侯办理。&mobile=15972035243&stime=&type=pt&sign=旅游局政务服务中心"   

private void PostRequest(string posturl)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(posturl);
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(posturl);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;
            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string Message = php.ReadToEnd();
            System.Console.Write(Message);
            System.Console.Read();
        }

2.第二种:参数是form-data的形式。

 public string PostRequest(string url,string name,string pwd,string content,string mobile,string stime,string sign,string type)
        {
            using (var client = new HttpClient())
            {
                using (var Content = new MultipartFormDataContent())
                {
                    client.BaseAddress = new Uri(url);
                    var txtContent = new ByteArrayContent(Encoding.UTF8.GetBytes(name));
                    txtContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        Name = "name"
                    };
                    Content.Add(txtContent);
                    txtContent = new ByteArrayContent(Encoding.UTF8.GetBytes(pwd));
                    txtContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        Name = "pwd"
                    };
                    Content.Add(txtContent);


                    txtContent = new ByteArrayContent(Encoding.UTF8.GetBytes(content));
                    txtContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        Name = "content"
                    };
                    Content.Add(txtContent);

                    txtContent = new ByteArrayContent(Encoding.UTF8.GetBytes(mobile));
                    txtContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        Name = "mobile"
                    };
                    Content.Add(txtContent);


                    txtContent = new ByteArrayContent(Encoding.UTF8.GetBytes(type));
                    txtContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        Name = "type"
                    };
                    Content.Add(txtContent);


                    txtContent = new ByteArrayContent(Encoding.UTF8.GetBytes(sign));
                    txtContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        Name = "sign"
                    };
                    Content.Add(txtContent);

                    txtContent = new ByteArrayContent(Encoding.UTF8.GetBytes(stime));
                    txtContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        Name = "stime"
                    };
                    Content.Add(txtContent);

                    var result = client.PostAsync("", Content).Result;
                    //this.textBox1.Text = result.Content.ReadAsStringAsync().Result;
                    return result.Content.ReadAsStringAsync().Result;
                }
            }

        }

3.第三种:参数是以x-www-form-urlencoded的形式传递

 [WebMethod]
        public string GetSsmInfo(string strSsm)
        {
            string sessionid = GetSessionId();
            string weburl = "http://58.33.97.180:8081/front/citizenCould/scanCertQrCode.do";
            string result = "";
            try
            {
                //入参
                string data = "sessionId="+ sessionid +
                    "&certQrCode="+ strSsm +
                    "&pos=市文旅局&use=取号机叫号";

                byte[] byteArray = Encoding.UTF8.GetBytes(data);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(weburl));
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.ContentLength = byteArray.Length;
                Stream newStream = webRequest.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();

                //接收返回信息:
                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                StreamReader aspx = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string sendTxt = aspx.ReadToEnd();

                JObject jo = JObject.Parse(sendTxt);
                result = jo["result"].ToString();
            }
            catch (Exception ex)
            {
                //sendTxt = ex.ToString();
                sendTxt = "获取接口错误";
                //wt.WriteMsg(ex.ToString(), "SendTxtSessionId");
            }
            return result;
        }

4.第四种:参数以application/json的形式传递

 [WebMethod]
        public string SsmInfo(string data)
        {
            string serviceAddress = "http://218.242.70.146/certificate/qrcode/scan/info/scanCertQrCode.do";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);

            request.Method = "POST";
            request.ContentType = "application/json";
            //string strContent = "{ \"account\": \"" + account + "\",\"password\": \"" + pwd + "\"}";
            string strContent = data; //参数data的格式就是上一句被注释的语句
            using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
            {
                dataStream.Write(strContent);
                dataStream.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();
            //解析josn
            JObject jo = JObject.Parse(retString);
            //sessionId = jo["sessionId"].ToString();
            //return sessionid;
            return retString;
        }

你可能感兴趣的:(asp.net,post调用接口,c#)