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


C# HttpClient.Disconnect方法代码示例

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


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

示例1: backgroundWorker1_DoWork

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            switch (e.Argument.ToString())
            {
                case "GetServerTime":
                    {
                        HttpClient client = new HttpClient();

                        client.Port = 80;
                        client.Host = "localhost";                        
                        client.Connect();
                        try
                        {
                            string result = client.Get("http://localhost/gettime");
                            Invoke(new MethodInvoker(delegate 
                            {
                                MessageBox.Show(string.Format("Server time is {0}", result));
                            }));
                        }
                        finally
                        {
                            client.Disconnect();
                        }
                    }
                    break;

                case "GetFile":
                    {
                        HttpClient client = new HttpClient();

                        client.Port = 80;
                        client.Host = "localhost";
                        client.Connect();
                        try
                        {
                            MemoryStream mem = new MemoryStream();
                            client.Get("http://localhost/getfile?name=" + textBox1.Text, mem);
                            if (client.Response.ResponseCode == 200)
                            {
                                Invoke(new MethodInvoker(delegate
                                {
                                    Activate();
                                    saveFileDialog.Title = string.Format("Select a target file for requested file {0}", textBox1.Text);
                                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                                    {
                                        File.WriteAllBytes(saveFileDialog.FileName, mem.ToArray());
                                    }
                                }));
                            }
                            else throw new Exception("Failed to get file. Code:" + client.Response.ResponseCode.ToString());
                        }
                        finally
                        {
                            client.Disconnect();
                        }
                    }
                    break;        
            }                        
        }
开发者ID:xuchuansheng,项目名称:GenXSource,代码行数:59,代码来源:ClientForm.cs

示例2: ProcessRequest

        private void ProcessRequest(HttpRequestInfo request, HttpResponseInfo response)
        {
            //lock (_syncRoot)
            //{
                HttpClient client = new HttpClient();
                try
                {

                    client.Host = request.Host;
                    client.Port = 80;
                    client.Connect();
                    try
                    {
                        client.Request.Accept = request.Accept;
                        client.Request.AcceptCharset = request.AcceptCharSet;
                        client.Request.AcceptEncoding = request.AcceptEncoding;
                        client.Request.AcceptLanguage = request.AcceptLanguage;
                       // client.Request.Authorization = request.Authentication;
                        client.Request.BasicAuthentication = request.BasicAuthentication;
                        client.Request.CacheControl = request.CacheControl;
                        client.Request.Connection = request.Connection;
                        client.Request.ContentEncoding = request.ContentEncoding;
                        client.Request.ContentLanguage = request.ContentLanguage;
                        client.Request.ContentLength = request.ContentLength;
                        //client.Request.ContentRangeEnd = request.ContentRangeEnd;
                        //client.Request.ContentRangeStart = request.ContentRangeStart;
                        client.Request.ContentType = request.ContentType;
                        //client.Request.ContentVersion = request.ContentVersion;
                        //client.Request.CustomHeaders = request.CustomHeaders;
                        client.Request.Date = request.Date;
                        client.Request.Expires = request.Expires;
                        client.Request.From = request.From;
                        client.Request.Host = request.Host;
                        client.Request.LastModified = request.LastModified;
                        client.Request.Method = HttpCommandEnum.Get;
                        client.Request.Password = request.Password;
                        client.Request.Pragma = request.Pragma;
                        client.Request.ProxyConnection = request.ProxyConnection;

                        //client.Request.RawHeaders.Clear();
                        //foreach (string header in request.RawHeaders)
                        //{
                        //    client.Request.RawHeaders.Add(header);
                        //}

                        client.Request.Referer = request.Referer;
                        client.Request.UserAgent = request.UserAgent;
                        client.Request.UserName = request.UserName;

                        MemoryStream mem = new MemoryStream();
                        client.Get(request.RequestURI, mem);

                        response.CacheControl = client.Response.CacheControl;
                        response.Connection = client.Response.Connection;
                        response.ContentEncoding = client.Response.ContentEncoding;
                        response.ContentLanguage = client.Response.ContentLanguage;
                        response.ContentLength = client.Response.ContentLength;
                        //response.ContentRangeEnd = client.Response.ContentRangeEnd;
                        //response.ContentRangeStart = client.Response.ContentRangeEnd;
                        response.ContentStream = client.Response.ContentStream;
                        response.ContentType = client.Response.ContentType;
                        //response.ContentVersion = client.Response.ContentVersion;
                        //response.CustomHeaders = client.Response.CustomHeaders;
                        response.Date = client.Response.Date;
                        response.Expires = client.Response.Expires;

                        response.LastModified = client.Response.LastModified;
                        response.Location = client.Response.Location;
                        response.Pragma = client.Response.Pragma;

                        request.RawHeaders.Clear();
                        foreach (string header in client.Response.RawHeaders)
                        {
                            request.RawHeaders.Add(header);
                        }

                        response.ResponseNo = client.Response.ResponseCode;
                        response.ResponseText = client.Response.ResponseText;
                        response.Server = client.Response.Server;
                        response.WWWAuthenticate = client.Response.WWWAuthenticate;
                    }

                    finally { client.Disconnect(); }
                }
                catch (Exception ex)
                {
                    response.ResponseNo = 500;
                    response.ContentText = string.Format("Exception: {0}", ex.Message);
                }
            //}
        }
开发者ID:xuchuansheng,项目名称:GenXSource,代码行数:91,代码来源:GetRequestHandler.cs


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