[c#] HttpClient does not exist in .net 4.0: what can I do?

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" ?

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;
            }
        }
    }
}

This question is related to c# .net webclient

The answer is


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


read this...

Portable HttpClient for .NET Framework and Windows Phone

see paragraph Using HttpClient on .NET Framework 4.0 or Windows Phone 7.5 http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx


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.

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


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

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.

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

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.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to webclient

Deciding between HttpClient and WebClient "A connection attempt failed because the connected party did not properly respond after a period of time" using WebClient POSTing JSON to URL via WebClient in C# HttpClient does not exist in .net 4.0: what can I do? Sending HTTP POST with System.Net.WebClient How to get a json string from url? How to post data to specific URL using WebClient in C# What difference is there between WebClient and HTTPWebRequest classes in .NET? How to get status code from webclient? How do I authenticate a WebClient request?