Google Translate API:有没有办法让http网页请求更快?

时间:2012-05-01 16:08:26

标签: c# http httpwebrequest httpwebresponse google-translate

我需要发出HTTP请求才能获取翻译文本。 如果我通过Internet Explorer手动完成它的速度很快;在一秒或更短的时间内,我得到了结果。

但出于某种原因,如果我使用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

1 个答案:

答案 0 :(得分:0)

生成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)Forbidden错误,所以我无法验证这是一个完整的修复,但试一试。至少这可以解决404错误问题。

相关问题