zoukankan      html  css  js  c++  java
  • 关于购物网站的支付接口(.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;

  • 相关阅读:
    抽丝剥茧设计模式- 责任链模式应用场景
    抽丝剥茧设计模式-工厂和抽象工厂模式
    抽丝剥茧设计模式-你真的懂单例模式吗?
    抽丝剥茧设计模式- 啰嗦几句
    生产环境ng配置
    Linux命令
    内容过滤报错
    user 报错
    cxlabel 显示不全的解决方法
    关于SQL事务的一些坑
  • 原文地址:https://www.cnblogs.com/hankstar/p/1481642.html
Copyright © 2011-2022 走看看