locked
Always receiving Bad Request response, even using microsoft sample... RRS feed

  • Question

  • Hi, I'm trying to use the Microsoft Translator API from Http

    I found the sample code here : http://msdn.microsoft.com/en-us/library/ff512387.aspx

    Here's my code: 

    			string text = "Use pixels to express measurements for padding and margins.";
    			string from = "en";
    			string to = "de";
    			
    			string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + WWW.EscapeURL(text) + "&from=" + from + "&to=" + to;
    
    			HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
    			httpWebRequest.Headers.Add("Authorization", "Bearer " + token.access_token);
    			Debug.Log(httpWebRequest.Headers);
    			WebResponse response = null;
    			try
    			{
    				response = httpWebRequest.GetResponse();
    				using (Stream stream = response.GetResponseStream())
    				{
    					System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
    					string translation = (string)dcs.ReadObject(stream);
    					Debug.Log(String.Format("Translation for source text '{0}' from {1} to {2} is", text, "en", "de"));
    					Debug.Log(translation);
    				}
    			}
    			catch (System.Exception e)
    			{
    				Debug.Log ("SimpleTranslate: " + e.Message);
    				throw e;
    			}
    

    I always get the exception : The remote server returned an error: (400) Bad Request.

    I don't understand why it is not working, it's a really simple sample... or is it a deprecated functionnality??

    Wednesday, February 5, 2014 8:15 PM

Answers

  • Try using this try/catch code to display more detail on the error. My own implementation of the code does work as expected. Also, are you obtaining a valid token? (see http://msdn.microsoft.com/en-us/library/hh454950.aspx)

    try
    {
    	...
    }
    catch (WebException e)
    {
    	using (WebResponse response = e.Response)
    	{
    		HttpWebResponse httpResponse = (HttpWebResponse) response;
    		Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
    		using (Stream data = response.GetResponseStream())
    		using (var reader = new StreamReader(data))
    		{
    			string text = reader.ReadToEnd();
    			Console.WriteLine(text);
    		}
    	}
    }

    Tuesday, June 24, 2014 7:21 AM

All replies

  • me also
    Tuesday, February 18, 2014 11:33 PM
  • Try using this try/catch code to display more detail on the error. My own implementation of the code does work as expected. Also, are you obtaining a valid token? (see http://msdn.microsoft.com/en-us/library/hh454950.aspx)

    try
    {
    	...
    }
    catch (WebException e)
    {
    	using (WebResponse response = e.Response)
    	{
    		HttpWebResponse httpResponse = (HttpWebResponse) response;
    		Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
    		using (Stream data = response.GetResponseStream())
    		using (var reader = new StreamReader(data))
    		{
    			string text = reader.ReadToEnd();
    			Console.WriteLine(text);
    		}
    	}
    }

    Tuesday, June 24, 2014 7:21 AM