Files
mycode/BaiduSearch/BaiduSearchApp/BaiduSearchApi.cs
2026-02-04 12:13:56 +08:00

131 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace BaiduSearchApp
{
public class BaiduSearchApi
{
private readonly string _apiKey;
private readonly HttpClient _httpClient;
private const string ApiUrl = "https://qianfan.baidubce.com/v2/ai_search/web_search";
public BaiduSearchApi(string apiKey)
{
_apiKey = apiKey;
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey);
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<SearchResponse> SearchAsync(string query, int pageSize = 50)
{
if (string.IsNullOrEmpty(query))
throw new ArgumentException("搜索内容不能为空", nameof(query));
var requestBody = new SearchRequest
{
Messages = new List<Message>
{
new Message { Content = query, Role = "user" }
},
SearchSource = "baidu_search_v2",
ResourceTypeFilter = new List<SearchResource>
{
new SearchResource { Type = "web", TopK = pageSize }
},
SearchRecencyFilter = "year"
};
var jsonContent = JsonConvert.SerializeObject(requestBody);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = await _httpClient.PostAsync(ApiUrl, content);
response.EnsureSuccessStatusCode();
string responseContent = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<SearchResponse>(responseContent);
}
}
// 请求相关类
public class SearchRequest
{
[JsonProperty("messages")]
public List<Message> Messages { get; set; }
[JsonProperty("search_source")]
public string SearchSource { get; set; }
[JsonProperty("resource_type_filter")]
public List<SearchResource> ResourceTypeFilter { get; set; }
[JsonProperty("search_recency_filter")]
public string SearchRecencyFilter { get; set; }
[JsonProperty("edition")]
public string Edition { get; set; } = "standard";
}
public class Message
{
[JsonProperty("content")]
public string Content { get; set; }
[JsonProperty("role")]
public string Role { get; set; }
}
public class SearchResource
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("top_k")]
public int TopK { get; set; }
}
// 响应相关类
public class SearchResponse
{
[JsonProperty("request_id")]
public string RequestId { get; set; }
[JsonProperty("references")]
public List<Reference> References { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("message")]
public string ErrorMessage { get; set; }
}
public class Reference
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("content")]
public string Content { get; set; }
[JsonProperty("date")]
public string Date { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("web_anchor")]
public string WebAnchor { get; set; }
[JsonProperty("website")]
public string Website { get; set; }
}
}