C# .net 4.0 中不存在 HttpClient:我该怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10308572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpClient does not exist in .net 4.0: what can I do?
提问by user1352869
Ok i edited my code i dont get errors but the messageBox.Show return nothing empty box. Maybe i need to add something in the referrer string ? I didnt understand what is the referrer and what should i put there. And i have a key already im using it in my code. The key is a long string and im using it in my code i dont use with the referrer. Why it dosent translate the word "hi" ?
好的,我编辑了我的代码,但没有出现错误,但 messageBox.Show 没有返回任何空框。也许我需要在引用字符串中添加一些内容?我不明白什么是推荐人,我应该在那里放什么。我有一个密钥已经在我的代码中使用它。密钥是一个长字符串,我在我的代码中使用它,我不与引用者一起使用。为什么它不翻译“嗨”这个词?
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;
}
}
}
}
采纳答案by McGarnagle
You can use WebClient.
Or (if you need more fine-grained control over the request) HttpWebRequest
Or, HttpClientin System.Net.Http.dll.
您可以使用WebClient。
或者(如果您需要对请求进行更细粒度的控制)HttpWebRequest
或者,System.Net.Http.dll 中的HttpClient。
Here's a "translation" to HttpWebRequest (needed rather than WebClient in order to set the referrer). (Uses System.Net and System.IO):
这是对 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
}
回答by cecilphillip
I've used HttpClient in .NET 4.0 applications on numerous occasions. If you are familiar with NuGet, you can do an Install-Package Microsoft.Net.Http to add it to your project. See the link below for further details.
我曾多次在 .NET 4.0 应用程序中使用过 HttpClient。如果您熟悉 NuGet,则可以执行 Install-Package Microsoft.Net.Http 将其添加到您的项目中。有关更多详细信息,请参阅下面的链接。
回答by stevethethread
Referring to the answers above, I am only adding this to help clarify things. It is possible to use HttpClient from .Net 4.0, and you have to install the package from here
参考上面的答案,我添加这个只是为了帮助澄清事情。可以使用 .Net 4.0 中的 HttpClient,您必须从这里安装软件包
However, the text is very confusion and contradicts itself.
但是,文字很混乱,自相矛盾。
This package is not supported in Visual Studio 2010, and is only required for projects targeting .NET Framework 4.5, Windows 8, or Windows Phone 8.1 when consuming a library that uses this package.
此包在 Visual Studio 2010 中不受支持,只有在使用使用此包的库时面向 .NET Framework 4.5、Windows 8 或 Windows Phone 8.1 的项目才需要此包。
But underneath it states that these are the supported platforms.
但在它下面声明这些是受支持的平台。
Supported Platforms:
支持的平台:
.NET Framework 4
Windows 8
Windows Phone 8.1
Windows Phone Silverlight 7.5
Silverlight 4
Portable Class Libraries
.NET 框架 4
视窗 8
视窗电话 8.1
Windows Phone Silverlight 7.5
银光4
便携式类库
Ignore what it ways about targeting .Net 4.5. This is wrong. The package is all about using HttpClient in .Net 4.0. However, you may need to use VS2012 or higher. Not sure if it works in VS2010, but that may be worth testing.
忽略定位 .Net 4.5 的方式。这是错误的。该包是关于在 .Net 4.0 中使用 HttpClient 的。但是,您可能需要使用 VS2012 或更高版本。不确定它是否适用于 VS2010,但这可能值得测试。
回答by acor3
read this...
读这个...
Portable HttpClient for .NET Framework and Windows Phone
用于 .NET Framework 和 Windows Phone 的便携式 HttpClient
see paragraph Using HttpClient on .NET Framework 4.0 or Windows Phone 7.5http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx
请参阅在 .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
回答by Jeremy Murray
Agreeing with TrueWill's comment on a separate answer, the best way I've seen to use system.web.http on a .NET 4 targeted project under current Visual Studio is Install-Package Microsoft.AspNet.WebApi.Client -Version 4.0.30506
同意 TrueWill 对单独答案的评论,我见过的在当前 Visual Studio 下的 .NET 4 目标项目上使用 system.web.http 的最佳方法是 Install-Package Microsoft.AspNet.WebApi.Client -Version 4.0.30506

