当前位置: 首页>>代码示例>>C#>>正文


C# ICredentials.IsNotNull方法代码示例

本文整理汇总了C#中ICredentials.IsNotNull方法的典型用法代码示例。如果您正苦于以下问题:C# ICredentials.IsNotNull方法的具体用法?C# ICredentials.IsNotNull怎么用?C# ICredentials.IsNotNull使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ICredentials的用法示例。


在下文中一共展示了ICredentials.IsNotNull方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SendRequest

        /// <summary>
        /// 发送到指定的URL的请求
        /// </summary>
        /// <param name="url">请求的URL</param>
        /// <param name="postData">要发送的数据</param>
        /// <param name="requestType">请求类型 POST GET</param>
        /// <param name="contentType">请求的内容类型。默认值是'application/x-www-form-urlencoded'.</param>
        /// <param name="userAgent">请求 UserAgent</param>
        /// <param name="keepAlive">KeepAlive true/false</param>
        /// <param name="credentials">使用的凭据</param>
        /// <param name="proxy">使用代理</param>
        /// <param name="timeout">超时</param>
        /// <param name="encoding">设置响应编码</param>
        /// <param name="postDataEncoding">设置POST数据编码</param>
        /// <returns>返回请求的数据</returns>
        public static string SendRequest(string url, string postData, string requestType, string contentType, string userAgent, bool? keepAlive, ICredentials credentials, IWebProxy proxy, int? timeout, Encoding encoding, Encoding postDataEncoding, WebHeaderCollection webHeaderCollection = null) {
            if (!String.IsNullOrEmpty(url)) {
                HttpWebRequest webReq;
                HttpWebResponse webResp = null;
                if (postDataEncoding == null) postDataEncoding = Encoding.UTF8;

                try {
                    webReq = (HttpWebRequest)WebRequest.Create(url);
                    webReq.Method = requestType;

                    if (webHeaderCollection.IsNotNull()) webReq.Headers = webHeaderCollection;
                    if (credentials.IsNotNull()) webReq.Credentials = credentials;
                    if (proxy.IsNotNull()) webReq.Proxy = proxy;
                    if (timeout.IsNotNull()) webReq.Timeout = (int)timeout;
                    webReq.ServicePoint.Expect100Continue = false;
                    //webReq.PreAuthenticate = true;
                    //webReq.AllowWriteStreamBuffering = true;
                    webReq.ContentType = !String.IsNullOrEmpty(contentType) ? contentType : "text/xml"; // "application/x-www-form-urlencoded";
                    if (!String.IsNullOrEmpty(userAgent)) webReq.UserAgent = userAgent;
                    if (keepAlive.IsNotNull()) webReq.KeepAlive = (bool)keepAlive;

                    byte[] data = !String.IsNullOrEmpty(postData) ? postDataEncoding.GetBytes(postData) : postDataEncoding.GetBytes(String.Empty);
                    webReq.ContentLength = data.Length;
                    if (requestType.ToUpper() != "GET") {
                        Stream writer = webReq.GetRequestStream();
                        writer.Write(data, 0, data.Length);
                        writer.Close();
                    }

                    webResp = (HttpWebResponse)webReq.GetResponse();

                    string response;
                    using (var receiveStream = webResp.GetResponseStream()) {
                        using (var readStream = new StreamReader(receiveStream, encoding))
                            response = readStream.ReadToEnd();
                    }

                    return response;
                } finally {
                    if (webResp.IsNotNull()) webResp.Close();
                }
            }

            throw new Exception("必须指定一个有效的URL。");
        }
开发者ID:pczy,项目名称:Pub.Class,代码行数:60,代码来源:HttpHelper.cs


注:本文中的ICredentials.IsNotNull方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。