谷歌翻译API:有没有办法使http web请求更快



我需要发出HTTP请求才能取回翻译后的文本。如果我通过IE浏览器手动执行此操作,它很快;在一秒钟或更短的时间内,我得到了结果。

但是出于某种原因,如果我用HttpWebRequest这样做,则需要更长的时间。

这是我尝试使用的代码。它确实工作得很好;我从服务器收到错误 404(未找到)。

有人可以为我修复此代码吗?我也不确定他们使用的编码是否足够好。

我有钥匙;只是没有在这里发布它。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web;
using System.IO;
namespace GoogleTranslateing
{
    public partial class Form1 : Form
    {
        string apiKey = "My Key";
        string sourceLanguage = "en";
        string targetLanguage = "de";
        string googleUrl;
        string textToTranslate = "hello world";
        public Form1()
        {
            InitializeComponent();
            googleUrl = "https://www.googleapis.com/language/translate/v2?key=" + apiKey + "&q=" + textToTranslate + "&source=" + sourceLanguage + "&target=" + targetLanguage;
            webRequest();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void webRequest()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(googleUrl);
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = textToTranslate;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
    }
}

有没有比使用WebRequestWebResponse更快的方法?

生成 404 错误是因为您尝试将 POST 数据发送到不允许它(也不需要它)的服务。将您的webRequest()更改为以下内容...

private void webRequest()
{
    // Create a request using a URL that can receive a post. 
    WebRequest request = WebRequest.Create(googleUrl);
    // Set the Method property of the request to POST^H^H^H^HGET.
    request.Method = "GET"; // <-- ** You're putting textToTranslate into the query string so there's no need to use POST. **
    //// Create POST data and convert it to a byte array.
    //string postData = textToTranslate;
    //byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    // ** Commenting out the bit that writes the post data to the request stream **
    //// Set the ContentLength property of the WebRequest.
    //request.ContentLength = byteArray.Length;
    //// Get the request stream.
    //Stream dataStream = request.GetRequestStream();
    //// Write the data to the request stream.
    //dataStream.Write(byteArray, 0, byteArray.Length);
    //// Close the Stream object.
    //dataStream.Close();
    // Get the response.
    WebResponse response = request.GetResponse();
    // Display the status.
    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    // Get the stream containing content returned by the server.
    Stream dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd();
    // Display the content.
    Console.WriteLine(responseFromServer);
    // Clean up the streams.
    reader.Close();
    dataStream.Close();
    response.Close();
}

我尚未在我的 Google API 帐户上启用结算功能;现在我收到 (403) 禁止错误,因此我无法验证这是否是完整的修复程序,但请尝试一下。至少这可以解决404错误问题。

最新更新