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


C# HttpWebRequest.SetHttpHeaders方法代码示例

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


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

示例1: Execute

        /// <summary>
        /// Execute is modified Curl_http() which in Curl gets called from the generic Curl_do() function when a HTTP
        /// request is to be performed. This creates and sends a properly constructed
        /// HTTP request.
        /// </summary>
        /// <param name="curl"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        internal override object Execute(PhpCurlResource curl, ref CURLcode result)
        {
            UserDefined data = curl.Data;
            HttpBitsUploader uploader;
            bool terminatedCorrectly = false;
            int redirectAttempts = 0;
            bool keepVerb = false;

            result = CURLcode.CURLE_OK;

            if (data.Str[(int)DupString.SET_URL] == null)
            {
                result = CURLcode.CURLE_COULDNT_CONNECT;
                return false;
            }

            Uri uri = Utils.CompleteUri(PhpVariable.AsString(data.Str[(int)DupString.SET_URL]),
                                            Scheme,
                                            data.UsePort);


            for (; ; )
            {
                request = (HttpWebRequest)HttpWebRequest.Create(uri);

                Curl_HttpReq httpreq = (redirectAttempts == 0) || keepVerb ? setRequestMethod(data) : Curl_HttpReq.GET;
                setTimeOut(data);
                setHttpVersion(data);

                request.AllowAutoRedirect = data.FollowLocation;
                request.MaximumAutomaticRedirections = data.MaxRedirects;

                if (data.Str[(int)DupString.USERAGENT] != null)
                    request.UserAgent = PhpVariable.AsString(data.Str[(int)DupString.USERAGENT]);

                if (data.Str[(int)DupString.SET_REFERER] != null)
                    request.Referer = PhpVariable.AsString(data.Str[(int)DupString.SET_REFERER]);

                if (data.Headers != null)
                    request.SetHttpHeaders(data.Headers);

                setProxy(data);
                setCredentials(data);
                setCookies(data);

                //ssl.VerifyPeer && ssl.VerifyHost == 2 is supported by default .NET
                // other values are currently unsupported

                if (data.Str[(int)DupString.CERT] != null)
                {
                    X509Certificate cert;
                    string certPath;

                    try
                    {
                        certPath = Path.Combine(ScriptContext.CurrentContext.WorkingDirectory, PhpVariable.AsString(data.Str[(int)DupString.SSL_CAFILE]));

                        if (data.Str[(int)DupString.KEY_PASSWD] == null)
                            cert = new X509Certificate(certPath);
                        else
                            cert = new X509Certificate(certPath, PhpVariable.AsString(data.Str[(int)DupString.KEY_PASSWD]));

                        request.ClientCertificates.Add(cert);
                    }
                    catch (CryptographicException)
                    {
                        //TODO: here are more caises to differentiate
                        result = CURLcode.CURLE_SSL_CACERT_BADFILE;
                        return false;
                    }

                }


                switch (httpreq)
                {
                    case Curl_HttpReq.POST_FORM:

                        //same as POST but we can send multiple items asform-data


                        if (data.HttpPostForm != null)
                        {
                            try
                            {
                                HttpFormDataUploader formUploader = new HttpFormDataUploader(request);
                                formUploader.UploadForm(data.HttpPostForm);
                            }
                            catch (WebException ex)
                            {
                                switch (ex.Status)
                                {
//.........这里部分代码省略.........
开发者ID:dw4dev,项目名称:Phalanger,代码行数:101,代码来源:CurlHttp.cs


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