winform 编写一个的http协议类库
时间: 2023-08-11 12:05:28 浏览: 211
首先,你需要在你的 WinForm 项目中添加一个类文件,命名为 HttpHelper.cs。然后,你可以按照以下步骤来编写 Http 协议类库:
1. 引入命名空间
在 HttpHelper.cs 文件的开头,添加以下代码:
```csharp
using System;
using System.IO;
using System.Net;
using System.Text;
```
这些命名空间将提供我们需要的类和方法。
2. 编写发送 Http 请求的方法
添加以下代码:
```csharp
public static string SendHttpGetRequest(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Timeout = 5000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
```
这个方法会发送一个 GET 请求,并返回响应内容的字符串。
3. 编写发送 Http POST 请求的方法
添加以下代码:
```csharp
public static string SendHttpPostRequest(string url, string postData)
{
byte[] data = Encoding.UTF8.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Timeout = 5000;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
```
这个方法会发送一个 POST 请求,并返回响应内容的字符串。
4. 编写上传文件的方法
添加以下代码:
```csharp
public static string UploadFile(string url, string filePath, string fileFieldName, string[] strings)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Timeout = 5000;
request.ContentType = "multipart/form-data; boundary=" + boundary;
using (Stream stream = request.GetRequestStream())
{
// 写入字符串参数
foreach (string str in strings)
{
byte[] bytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
stream.Write(bytes, 0, bytes.Length);
bytes = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"" + str.Split('=')[0] + "\"\r\n\r\n");
stream.Write(bytes, 0, bytes.Length);
bytes = Encoding.UTF8.GetBytes(str.Split('=')[1] + "\r\n");
stream.Write(bytes, 0, bytes.Length);
}
// 写入文件
byte[] fileBytes = File.ReadAllBytes(filePath);
byte[] endBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
string fileHeader = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n", fileFieldName, Path.GetFileName(filePath));
byte[] fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeader);
stream.Write(Encoding.UTF8.GetBytes("--" + boundary + "\r\n"), 0, Encoding.UTF8.GetByteCount("--" + boundary + "\r\n"));
stream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
stream.Write(fileBytes, 0, fileBytes.Length);
stream.Write(endBytes, 0, endBytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
```
这个方法会上传一个文件,并带上一些字符串参数。其中,fileFieldName 参数指定了文件在服务器上的字段名,strings 参数指定了其他的字符串参数。
5. 使用类库发送 Http 请求
现在,你已经编写好了一个简单的 Http 协议类库。你可以在你的 WinForm 项目中使用它来发送 Http 请求。
例如,你可以在按钮的 Click 事件中添加以下代码:
```csharp
private void button1_Click(object sender, EventArgs e)
{
string url = "http://www.example.com/api/test";
// 发送 GET 请求
string response1 = HttpHelper.SendHttpGetRequest(url);
// 发送 POST 请求
string postData = "username=admin&password=123456";
string response2 = HttpHelper.SendHttpPostRequest(url, postData);
// 上传文件
string filePath = @"C:\Users\Administrator\Desktop\test.jpg";
string[] strings = new string[] { "uid=1", "token=abc123" };
string response3 = HttpHelper.UploadFile(url, filePath, "file", strings);
}
```
这个例子展示了如何发送 GET 请求、POST 请求和上传文件。你可以根据自己的需求来使用这个类库。
阅读全文