.net调用接口Post,Get数据实例,很详细

18 篇文章 0 订阅

 var userId = HttpContext.Current.Request["userId"];
                var imgUrl = HttpContext.Current.Request["imgUrl"];
                 
                Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表
                parameters.Add("crop_image", "1");
                parameters.Add("image_url", imgUrl);
                string url = "http://www.baidu.com"; 
                HttpWebRequest request = null;
                HttpWebResponse response = null;
                Stream reqStream = null;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "post"; //设置为post请求
                request.ReadWriteTimeout = 5000;
                request.KeepAlive = false;
                request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
                byte[] postData = Encoding.UTF8.GetBytes(Common.Utils.BuildQuery(parameters, "utf8")); //使用utf-8格式组装post参数
                reqStream = request.GetRequestStream();
                reqStream.Write(postData, 0, postData.Length);
                response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("UTF-8"));
                //返回内容
                string retUrl = myStreamReader.ReadToEnd();

 

 

 

 

public static string BuildQuery(IDictionary<string, string> parameters, string encode)
        {
            StringBuilder postData = new StringBuilder();
            bool hasParam = false;
            IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
            while (dem.MoveNext())
            {
                string name = dem.Current.Key;
                string value = dem.Current.Value;
                // 忽略参数名或参数值为空的参数
                if (!string.IsNullOrEmpty(name))
                {
                    if (hasParam)
                    {
                        postData.Append("&");
                    }
                    postData.Append(name);
                    postData.Append("=");
                    if (encode == "gb2312")
                    {
                        postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
                    }
                    else if (encode == "utf8")
                    {
                        postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
                    }
                    else
                    {
                        postData.Append(value);
                    }
                    hasParam = true;
                }
            }
            return postData.ToString();
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
泛微OA是一款办公自动化软件,它提供了丰富的接口来方便与其他系统进行集成。接口调用是指通过HTTP/HTTPS请求的方式,实现与泛微OA系统进行数据交互与操作。 在进行接口调用时,首先需要进行认证,通常使用用户名密码或者Token进行身份验证。接口文档中详细列出了每个接口的参数、请求方法、请求URL、请求头、请求体等信息。接口调用实例可以包括获取用户信息、提交表单、查询流程状态、上传附件等多种操作。 以获取用户信息接口为例,我们可以通过发送HTTP GET请求到指定的URL,附带合适的请求头和身份验证信息,就能获取到指定用户的个人信息,包括姓名、工号、部门、职位等详细信息。而提交表单接口则需要发送HTTP POST请求,将表单数据以JSON格式传递到指定的URL,通过接口返回的结果来判断提交是否成功。 另外,泛微OA还提供了一些回调接口,用于接收系统内事件通知,例如审批流程的状态变更、公文的审批结果等。这些接口可以用于实现自定义的业务逻辑,与其他系统进行数据同步、触发流程等操作。 总的来说,泛微OA接口调用实例丰寵多样,可以根据具体的需求进行定制化开发,实现与其他系统的快速集成和数据交互。通过合理的调用接口,可以更好地提高工作效率,降低系统之间的数据隔离,实现信息共享与协同办公。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值