60

好的,我编辑了我的代码我没有收到错误,但 messageBox.Show 没有返回任何空框。也许我需要在引用字符串中添加一些东西?我不明白什么是推荐人以及我应该放什么。而且我已经在我的代码中使用了一个密钥。密钥是一个长字符串,我在我的代码中使用它,我不与引用者一起使用。为什么不翻译“hi”这个词?

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.Globalization;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;




namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private JavaScriptSerializer _Serializer = new JavaScriptSerializer();

        public Form1()
        {
            InitializeComponent();
            string f = TranslateText("hi", "English", "German", "", "");
            MessageBox.Show(f);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        public string TranslateText(string inputText, string sourceLanguage, string destinationLanguage, string referrer, string apiKey)
        {
                string requestUrl = string.Format(
                    "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={0}&langpair={1}|{2}&key={3}", 
                    HttpUtility.UrlEncode(inputText), 
                    sourceLanguage.ToLowerInvariant(), 
                    destinationLanguage.ToLowerInvariant(), 
                    apiKey
                );

                try
                {
                    HttpWebRequest http = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                    http.Referer = referrer;
                    HttpWebResponse response = (HttpWebResponse)http.GetResponse();
                    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                    {
                        string responseJson = sr.ReadToEnd();
                        var translation = this._Serializer.Deserialize<Milkshake.Integration.Google.GoogleAjaxResponse<Milkshake.Integration.Google.Translate.TranslationResponse>>(responseJson);

                        if (translation != null && translation.ResponseData != null && translation.ResponseData.ResponseStatus == HttpStatusCode.OK)
                        {
                            return translation.ResponseData.TranslatedText;
                        }
                        else
                        {
                            return String.Empty;
                        }
                    }
                }
            catch
                {
                    return String.Empty;
            }
        }
    }
}
4

5 回答 5

80

我曾多次在 .NET 4.0 应用程序中使用 HttpClient。如果您熟悉 NuGet,则可以执行 Install-Package Microsoft.Net.Http 将其添加到您的项目中。有关详细信息,请参阅下面的链接。

http://nuget.org/packages/Microsoft.Net.Http

于 2012-08-10T04:57:57.903 回答
49

这是对 HttpWebRequest 的“翻译”(需要而不是 WebClient 来设置引荐来源网址)。(使用 System.Net 和 System.IO):

    HttpWebRequest http = (HttpWebRequest)HttpWebRequest.Create(requestUrl))
    http.Referer = referrer;
    HttpWebResponse response = (HttpWebResponse )http.GetResponse();
    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
    {
        string responseJson = sr.ReadToEnd();
        // more stuff
    }
于 2012-04-25T02:29:35.243 回答
19

参考上面的答案,我只是添加这个来帮助澄清事情。可以使用 .Net 4.0 中的 HttpClient,您必须从此处安装包

但是,文本非常混乱,并且自相矛盾。

Visual Studio 2010 不支持此包,只有在使用使用此包的库时,面向 .NET Framework 4.5、Windows 8 或 Windows Phone 8.1 的项目才需要此包。

但在它下面指出这些是受支持的平台。

支持的平台:

  • .NET 框架 4

  • 视窗 8

  • 视窗电话 8.1

  • Windows 手机银光 7.5

  • 银光4

  • 便携式类库

忽略它以 .Net 4.5 为目标的方式。这是错误的。该软件包是关于在 .Net 4.0 中使用 HttpClient 的。但是,您可能需要使用 VS2012 或更高版本。不确定它是否适用于 VS2010,但这可能值得测试。

于 2014-11-05T16:37:45.753 回答
2

读这个...

用于 .NET Framework 和 Windows Phone 的便携式 HttpClient

请参阅段落在 .NET Framework 4.0 或 Windows Phone 7.5 上使用 HttpClient http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone .aspx

于 2015-08-25T13:01:48.410 回答
1

同意 TrueWill 对单独答案的评论,我见过的在当前 Visual Studio 下的 .NET 4 目标项目上使用 system.web.http 的最佳方式是Install-Package Microsoft.AspNet.WebApi.Client -Version 4.0.30506

于 2018-07-12T18:29:12.830 回答