我想POST从C#到DeepLAPI
I want to POST from C # to DeepL API
我想POST从C#到DeepLAPI。
它只是行不通。
谁能告诉我怎么做?
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api-free.deepl.com/v2/translate");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Headers.Add("auth_key", "auth_key");
httpWebRequest.Headers.Add("target_lang", "JA");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
strSendJson = "{" +
"\"text\":\"" + strData.Trim() + "\"," +
"}";
}
DeepL 模拟器是一个很好的起点:https://www.deepl.com/docs-api/simulator
当使用 POST 时,auth_key
只是表单字段中的另一个字段,而不是 header。
Because the content type is specified as x-www-form-urlencoded
you are expected to send the data as a url encoded form, not JSON. Over the wire this should look something like:
auth_key=[yourAuthKey]&text=Hello, world&target_lang=JA
下面显示了如何使用 HttpClient 从 C# 发送此 POST 请求:
string authKey = "~MyAuthKey~";
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
Dictionary<string, string> data = new Dictionary<string, string>
{
{ "auth_key", authKey },
{ "text", strData.Trim() },
{ "target_lang", "JA" }
};
var response = await client.PostAsync("https://api-free.deepl.com/v2/translate",
new System.Net.Http.FormUrlEncodedContent(data));
response.EnsureSuccessStatusCode();
var json = response.Content.ReadAsStringAsync().Result;
deeplResponseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<DeepResponse>(json);
foreach(var tx in deeplResponseObj.translations)
Console.WriteLine("\"{0}\" (from {1})", tx.text, tx.detected_source_language);
以下 class 定义有助于反序列化响应:
public class DeepResponse
{
List<DeepTranslation> translations { get;set; } = new List<DeepTranslation>();
}
public class DeepTranslation
{
public string detected_source_language { get;set; }
public string text { get;set; }
}
我想POST从C#到DeepLAPI。 它只是行不通。 谁能告诉我怎么做?
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api-free.deepl.com/v2/translate");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Headers.Add("auth_key", "auth_key");
httpWebRequest.Headers.Add("target_lang", "JA");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
strSendJson = "{" +
"\"text\":\"" + strData.Trim() + "\"," +
"}";
}
DeepL 模拟器是一个很好的起点:https://www.deepl.com/docs-api/simulator
当使用 POST 时,auth_key
只是表单字段中的另一个字段,而不是 header。
Because the content type is specified as
x-www-form-urlencoded
you are expected to send the data as a url encoded form, not JSON. Over the wire this should look something like:auth_key=[yourAuthKey]&text=Hello, world&target_lang=JA
下面显示了如何使用 HttpClient 从 C# 发送此 POST 请求:
string authKey = "~MyAuthKey~";
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
Dictionary<string, string> data = new Dictionary<string, string>
{
{ "auth_key", authKey },
{ "text", strData.Trim() },
{ "target_lang", "JA" }
};
var response = await client.PostAsync("https://api-free.deepl.com/v2/translate",
new System.Net.Http.FormUrlEncodedContent(data));
response.EnsureSuccessStatusCode();
var json = response.Content.ReadAsStringAsync().Result;
deeplResponseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<DeepResponse>(json);
foreach(var tx in deeplResponseObj.translations)
Console.WriteLine("\"{0}\" (from {1})", tx.text, tx.detected_source_language);
以下 class 定义有助于反序列化响应:
public class DeepResponse
{
List<DeepTranslation> translations { get;set; } = new List<DeepTranslation>();
}
public class DeepTranslation
{
public string detected_source_language { get;set; }
public string text { get;set; }
}