关于购物网站的支付接口(.NET)

最近的项目涉及到接口方面,像支付宝在线支付啥的

通常会拿到一个接口文档,上面有 接口参数,接口编码方式,接口地址和一些额外的说明

接受部分是一串字符串,上面有返回值的说明或者一个XML语句

POST方式的输出或者接收

using System.Net;
using System.IO;

public static string GetBackNum()
        {
            string url = "接口地址";

           

            string PostData = "接口参数地址";

        Encoding gb = Encoding.GetEncoding("GB2312");
        byte[] data = gb.GetBytes(PostData);

        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.ContentLength = data.Length;
        Stream newStream = myRequest.GetRequestStream();
        newStream.Write(data, 0, data.Length);
        newStream.Close();

        HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
        string backstr = sr.ReadToEnd();//获取接口另一边的返回值,通常是100 104啥的,具体要看接口说明
        sr.Close();
        res.Close();

        return backstr;

        }

特别要注意的是

PostData  这个传递过去的参数,里面不能含有 &   等非法符号,不然会传送不成功,所以传递的时候尽量用一个类去过滤一下

iPostData  =d=123&pwd=234&copy=56dds;

原文地址:https://www.cnblogs.com/hankstar/p/1481642.html