Ok i edytowałem mój kod, ale nie dostaję błędów, ale messageBox.Show nic nie zwraca pustego pola. Może muszę dodać coś w ciągu odsyłającym? Nie rozumiem, co to jest polecający i co powinienem tam umieścić. I mam klucz już używam go w moim kodzie. Klucz jest długim ciągiem i używam go w moim kodzie, którego nie używam ze stroną odsyłającą. Dlaczego to wyrażenie tłumaczy słowo "cześć"?HttpClient nie istnieje w .net 4.0: co mogę zrobić?
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;
}
}
}
}
tytułem tego pytania jest bardzo jasne, ale samo pytanie jest niejasne. Wprawdzie pokazuje wysiłek badawczy, ale to wszystko. Powinien istnieć przynajmniej kontekst kontekstu, zamiast pozwolić czytelnikowi się domyślić. Widziałem, że zredagowałeś własne pytanie, powinieneś ponownie, ale upewnij się, że wynik końcowy jest czytelny dla nowo przybyłego. Pytania mają wartość na dłuższą metę, ale uczestnicy muszą to umożliwić. –