c# - .net 4.0 中不存在 HttpClient : what can I do?

标签 c# .net webclient

好的,我编辑了我的代码,我没有收到错误,但是 messageBox.Show 没有返回任何空框。 也许我需要在引用字符串中添加一些东西?我不明白什么是推荐人,我应该放在那里。我已经在我的代码中使用了一把 key 。 key 是一个长字符串,我在我的代码中使用它,但我不与引荐来源网址一起使用。为什么它不翻译“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;
            }
        }
    }
}

最佳答案

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

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

关于c# - .net 4.0 中不存在 HttpClient : what can I do?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10308572/

相关文章:

c# - Console.WriteLine语句中的“控制台”是什么?

.net - 如何以编程方式确定桌面的大小?

c# - 使用 webClient C# 在异步文件下载中将文件名传递给 DownloadFileCompleted

c# - 数据网格和设计器的奇怪 VS2010 行为

c# - 对 MVC Controller 感到好奇吗?

c# - 属性构造函数在运行时的什么时间运行?

.net - Azure 逻辑应用程序 : How to Send an Email with one or more attachments after getting the content from Blob storage?

c# - WebClient.DownloadDataAsync 真的会抛出异常吗?

c# - 从网址下载后播放声音文件

c# - 如何做最小起订量可见的内部接口(interface)?