BestHttp post Header
时间: 2023-10-19 11:36:13 浏览: 90
To set headers for a POST request using the Best HTTP library in C#, you can use the `HttpRequest` class. Here's an example of how to add headers to a POST request:
```csharp
using BestHTTP;
using BestHTTP.Forms;
using UnityEngine;
public class HttpPostWithHeadersExample : MonoBehaviour
{
void Start()
{
// Create a new form
var form = new HTTPForm();
// Add form fields if needed
form.AddField("key1", "value1");
form.AddField("key2", "value2");
// Create a new request
var request = new HTTPRequest(new System.Uri("http://example.com"), HTTPMethods.Post, OnRequestFinished);
// Add headers to the request
request.SetHeader("Content-Type", "application/x-www-form-urlencoded");
request.SetHeader("Authorization", "Bearer YOUR_TOKEN");
// Assign the form data to the request
request.RawData = form.ToByteArray();
// Send the request
request.Send();
}
void OnRequestFinished(HTTPRequest request, HTTPResponse response)
{
if (response.IsSuccess)
{
Debug.Log("Request successful!");
Debug.Log("Response: " + response.DataAsText);
}
else
{
Debug.LogError("Request failed!");
Debug.LogError("Error: " + response.Message);
}
}
}
```
In this example, we create a new `HTTPRequest` and set the method to `Post` as the second parameter. We then add headers using the `SetHeader` method. Finally, we assign the form data (if any) to the request using `request.RawData` and send the request with `request.Send()`. The response is handled in the `OnRequestFinished` callback function.
Replace `http://example.com` with your actual URL, and modify the headers and form fields according to your requirements.
阅读全文