本文整理汇总了C#中HttpClient.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.Connect方法的具体用法?C# HttpClient.Connect怎么用?C# HttpClient.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpClient
的用法示例。
在下文中一共展示了HttpClient.Connect方法的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;
}
}
示例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);
}
//}
}