简体   繁体   English

如何使用简单的C#WebClient使用相同的密钥(使用Google Translate)发布多个值

[英]How do I POST multiple values with the same key (to Google Translate) using the simple C# WebClient

I wish to send multiple values to the Google Translate API using the simple syntax provided by the C# WebClient . 我希望使用C# WebClient提供的简单语法向Google Translate API发送多个值。 To send multiple values to the API, each value has to have the same query-string key, for example: q=value1&q=value2 . 要向API发送多个值,每个值必须具有相同的查询字符串键,例如: q=value1&q=value2

I cannot use the default GET mechanism and simply put these values on the query-string because some of my values are too large. 我不能使用默认的GET机制,只是将这些值放在查询字符串上,因为我的一些值太大了。 I therefore have to POST these values making sure I set the X-HTTP-Method-Override header. 因此,我必须POST这些值,确保我设置X-HTTP-Method-Override标头。

The problem is, to POST my values I need to use the WebClient.UploadValues() method which expects the values to be presented as a NameValueCollection . 问题是,要POST我的值,我需要使用WebClient.UploadValues()方法,该方法要求将值显示为NameValueCollection Multiple values with the same key are supported by the NameValueCollection but not in a way that the Google Translate API will recognise as separate values (it creates a simple comma delimited set of values held under a single key unique key). NameValueCollection支持具有相同键的多个值,但不支持Google Translate API将其识别为单独的值(它创建一个以单个键唯一键保存的简单逗号分隔值集)。

How do I POST multiple values, each with the same key, using the WebClient class? 如何使用WebClient类发布多个值,每个值都使用相同的键?

For further reading see: 进一步阅读请参阅:

To do this you can use the WebClient.UploadString() method, although there are a couple of gotchas to note. 要做到这一点,你可以使用WebClient.UploadString()方法,虽然有一些问题需要注意。 First some code: 首先是一些代码:

using (var webClient = new WebClient())
{
    webClient.Encoding = Encoding.UTF8;
    webClient.Headers.Add("X-HTTP-Method-Override", "GET");
    webClient.Headers.Add("content-type", "application/x-www-form-urlencoded");
    var data = string.Format("key={0}&source={1}&target={2}&q={3}&q={4}", myApiKey, "en", "fr", urlEncodedValue1, urlEncodedvalue2);
    try
    {
        var json = webClient.UploadString(GoogleTranslateApiUrl, "POST", data);
        var result = JsonConvert.DeserializeObject<dynamic>(json);
        translatedValue1 = result.data.translations[0].translatedText;
        translatedValue2 = result.data.translations[1].translatedText;
    }
    catch (Exception ex)
    {
        loggingService.Error(ex.Message);
    }
}

You can see that I am formatting the data to be sent to the Google Translate API as a application/x-www-form-urlencoded string. 您可以看到我正在格式化要作为application/x-www-form-urlencoded字符串发送到Google Translate API的数据。 This allows multiple values with the same key to be formatted together. 这允许将具有相同键的多个值格式化在一起。

To post this correctly You must remember to set the WebClient.Encoding property, in my case to UTF8 , as the WebClient converts the string to be uploaded into an array of bytes before posting them. 要正确发布您必须记住将WebClient.Encoding属性(在我的情况下)设置为UTF8 ,因为WebClient会在发布之前将要上载的字符串转换为字节数组。

You must also remember to set the content-type header to application/x-www-form-urlencoded thus ensuring the payload is correctly packaged. 您还必须记住将content-type标头设置为application/x-www-form-urlencoded从而确保正确打包有效负载。

Finally you have to remember to urlencode the values to to be translated. 最后,您必须记住要将要翻译的值进行urlencode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM