C# Google Translate API:有没有办法让http web请求更快?
我需要发出HTTP请求以获取翻译后的文本。 如果我通过Internet Explorer手动操作,速度很快;在一秒钟或更短的时间内,我得到了结果 但由于某些原因,如果我使用HttpWebRequest,则需要更长的时间 这是我试图使用的代码。它工作得很好;从服务器上找不到错误404 有人能帮我修一下这个密码吗?我也不确定他们使用的编码是否足够好 我有钥匙;只是没有在这里发表C# Google Translate API:有没有办法让http web请求更快?,c#,http,httpwebrequest,httpwebresponse,google-translate,C#,Http,Httpwebrequest,Httpwebresponse,Google Translate,我需要发出HTTP请求以获取翻译后的文本。 如果我通过Internet Explorer手动操作,速度很快;在一秒钟或更短的时间内,我得到了结果 但由于某些原因,如果我使用HttpWebRequest,则需要更长的时间 这是我试图使用的代码。它工作得很好;从服务器上找不到错误404 有人能帮我修一下这个密码吗?我也不确定他们使用的编码是否足够好 我有钥匙;只是没有在这里发表 using System; using System.Collections.Generic; using System.
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();
}
}
}
还有比使用WebRequest和WebResponse更快的方法吗?由于您试图将POST数据发送到一个不允许也不需要它的服务,所以产生了404错误。将您的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();
}
我还没有在我的谷歌API帐户上启用计费功能;现在我得到了一个403禁止的错误,所以我无法验证这是否是一个完整的修复,但请尝试一下。至少这解决了404错误问题。是性能问题还是404错误?或者两者都有?如果你得到一个404,不管它有多快,你需要先修复它。您在querystring和POST正文中都包含了textToTranslate,这是正确的吗?如果您还不确定速度,那么为什么您的问题标题是关于速度的?在这里,你需要一次解决一个问题。从404问题开始,然后看看速度是否仍然是一个问题。404将导致延迟。如果将googleUrl的值粘贴到浏览器中,会发生什么情况?它是否也是404?