/// <summary>
        /// 传输方法
        /// </summary>
        /// <param name="url">接口地址</param>
        /// <param name="ms">序列化后的数据</param>
        /// <returns></returns>
        public static JObject ResponseBind(string url, string buffer)
        {
            SecurityUtils su = new SecurityUtils();//给值
            IDictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("request", buffer);
            string strReturn = su.ReturnActionFromXml(url, parameters);
            JObject jo = (JObject)JsonConvert.DeserializeObject(strReturn);
            string responsestr = jo["response"] != null ? jo["response"].ToString().Trim() : "";//执行结果
            JObject jo2 = (JObject)JsonConvert.DeserializeObject(responsestr);
            return jo2;
        }

        public string ReturnActionFromXml(string url, IDictionary<string, string> parameters)
        {
            string xml;
            HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(url, parameters, null, null, Encoding.UTF8, null);
            using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                xml = sr.ReadToEnd();
            }
            return xml;
        }
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web;
namespace Dscf.Client.InformationAuditWeb.Handler
{
    public class HttpWebResponseUtility
    {
        private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
        public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.UserAgent = DefaultUserAgent;
            if (!string.IsNullOrEmpty(userAgent))
            {
                request.UserAgent = userAgent;
            } if (timeout.HasValue)
            {
                request.Timeout = timeout.Value;
            } if (cookies != null)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cookies);
            }
            return request.GetResponse() as HttpWebResponse;
        }
        public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies)
        {
            if (string.IsNullOrEmpty(url))
            { throw new ArgumentNullException("url"); }
            if (requestEncoding == null)
            {
                throw new ArgumentNullException("requestEncoding");
            }
            HttpWebRequest request = null;             //如果是发送HTTPS请求     
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version10;
            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            if (!string.IsNullOrEmpty(userAgent))
            {
                request.UserAgent = userAgent;
            }
            else
            {
                request.UserAgent = DefaultUserAgent;
            } if (timeout.HasValue)
            {
                request.Timeout = timeout.Value;
            } if (cookies != null)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cookies);
            }
            //如果需要POST数据  
            if (!(parameters == null || parameters.Count == 0))
            {
                StringBuilder buffer = new StringBuilder(); int i = 0;
                foreach (string key in parameters.Keys)
                {
                    if (i > 0)
                    {
                        buffer.AppendFormat("&{0}={1}", key, parameters[key]);
                    }
                    else
                    {
                        buffer.AppendFormat("{0}={1}", key, parameters[key]);
                    } i++;
                }
                byte[] data = requestEncoding.GetBytes(buffer.ToString());
                using (Stream stream = request.GetRequestStream())
                { stream.Write(data, 0, data.Length); }
            } return request.GetResponse() as HttpWebResponse;
        }
        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受    
        }
    }
}

C# 后台请求web服务方法的更多相关文章

  1. svn + nginx unit + python3自动化发布web服务方法

    本周将python web服务管理更换成nginx unit以后发现接口性能有了明显的提升,访问速度快了不少.不过有个很大的问题就是使用svn自动化发布以后,服务并没有刷新使用新的代码运行,而又不懂得 ...

  2. ajax请求web服务返回json格式

    由于.net frameword3.5以上添加了对contenttype的检查,当ajax发送请求时,如果设置了contenttype为json,那么请求webservice时,会自动将返回的内容转为 ...

  3. 监控web服务方法

    本地监控:端口  netstat -anltup | grep 80  nmap ip -p 80 telnet ip:80 lsof -i :80|wc -l 进程 ps -ef| grep ngi ...

  4. ajax后台请求两种方法(js和jQuery)

    (1)js的ajax var xmlHttp; if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ...

  5. webServices与Web服务

    本篇的内容在MSND中标注已是一项旧技术,而取而代之的是WCF, 那么我也放弃吧!但是这个属于Web服务的范畴,而WCF本质上也是一个Web服务来的,所以对于基础的东西还是不变的.那么这次就着重看看这 ...

  6. WCF:为 SharePoint 2010 Business Connectivity Services 构建 WCF Web 服务(第 1 部分,共 4 部分)

    转:http://msdn.microsoft.com/zh-cn/library/gg318615.aspx 摘要:通过此系列文章(共四部分)了解如何在 Microsoft SharePoint F ...

  7. 12个强大的Web服务测试工具

    在过去的几年中,web服务或API的普及和使用有所增加. web服务或API是程序或软件组件的集合,可以帮助应用程序进行交互或通过形成其他应用程序或服务器之间的连接执行一些进程/事务处理.基本上有两种 ...

  8. Web服务器之Nginx详解(理论部分)

    大纲 一.前言 二.Web服务器提供服务的方式 三.多进程.多线程.异步模式的对比 四.Web 服务请求过程 五.Linux I/O 模型 六.Linux I/O 模型具体说明 七.Linux I/O ...

  9. 【转】Web服务器之Nginx详解(理论部分)

    大纲 一.前言 二.Web服务器提供服务的方式 三.多进程.多线程.异步模式的对比 四.Web 服务请求过程 五.Linux I/O 模型 六.Linux I/O 模型具体说明 七.Linux I/O ...

随机推荐

  1. codeforces 336D. Vasily the Bear and Beautiful Strings 组合数学 dp

    题意: 给出n,m,g,求好串的个数 0 <= n,m <= 10^5,n + m >= 1,0 <= g <= 1 好串的定义: 1.只由0,1组成,并且恰好有n个0, ...

  2. input file 上传文件

    面试的时候遇到一个问题,要求手写的方式上传文件. 本来觉得很简单,但是结果怎么也成功不了. 前台: <form ID="form1" action="AcceptF ...

  3. 文件 FIFO队列

    <?php /** * Filefifo.php 文件型FIFO队列 */ class Filefifo { /** * $_file_data, 数据文件的路径 */ private $_fi ...

  4. Condition的优点

    那么引入本篇的主角,Condition,Condition 将 Object 监视器方法(wait.notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与任意 Lock 实现 ...

  5. VI下删除所有内容

    让光标在一个位置 , 如果全删就放在问价开始的位置 , 在非编辑的状态下输入dG(注意大小写) , 这样光标之后的所有行都会被删掉

  6. How to push your code in git

    1. display all the branches git branch -a 2. delete branches git br -d <branch> # 删除某个分支 git b ...

  7. 97、进入ScrollView根布局页面,直接跳到页面底部,不能显示顶部内容

    API使用:http://www.cnblogs.com/over140/archive/2011/01/27/1945964.html 以ScrollView为根的部局,不能从顶部显示其包含的页面内 ...

  8. crm SSRS 报表 导出格式控制

    如果是使用的网页嵌入ReportView的方式的,可以在aspx上加入js来控制导出格式: <script src="js/jquery-1.9.0.js"></ ...

  9. 【cl】sikuli启动不了

    公司电脑:win7+64位 问题:点击sikuli_ide没有反应 卸载了,启动电脑,重新安装.

  10. java整数类型

    1.整数类型:byte占8位,short 占16位,int占32位,long占64位. 2.对于long类型的值,若赋值给的值大于int类型的最大值或小于int型的最小值,则需要在数字后加L或l,表示 ...