本文介绍了HTTP API:为什么Translate和TranslateArray会返回不同的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我真的需要TranslateArray来处理我的Java类,因为必须将每个短语翻译为单独的HTTP事务才是低效的。 无论如何。如果我创建以下URL,将我的句子从法语翻译成德语,它就可以了。 网址I really need TranslateArray to work with my Java class as having to translate every single phrase as a separate HTTP transaction is just inefficient.  Anyway. If I create the following URL, to translate my sentence from French to German, it works. The URL is http://请求api.microsofttranslator.com/v2/Http.svc/Translate?appId=XX&text=Ma+r%C3% A9ponse + est + traduite& from = fr& to = dehttp://request to api.microsofttranslator.com/v2/Http.svc/Translate?appId=XX&text=Ma+r%C3%A9ponse+est+traduite&from=fr&to=de响应回来了:Meine Antwort wirdü bersetztThe response comes back : Meine Antwort wird übersetzt很好。现在使用TranslateArray函数。首先,基于另一个线程的提示,我尝试URL编码,所以我正在传递**完全**与上面相同的字符串:Fine. Now using the TranslateArray function. First, based on a hint on another thread, I try URL encoding so I'm passing **EXACTLY** the same string as above:这是XML:< TranslateArrayRequest><TranslateArrayRequest> < AppId> XXX< / AppId> <AppId>XXX</AppId>< From> fr< /来自><From>fr</From><选项><Options>< ContentType xmlns =" http://schemas.datacontract.org/2004/07/ Microsoft.MT.Web.Service.V2"> text / plain< / ContentType><ContentType xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/plain</ContentType>< / Options></Options>< Texts><Texts>< string xmlns =" http://schemas.microsoft.com/2003/10/Serialization/Arrays"> Ma + r%C3%A9ponse + \\ test + traduite< / string><string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Ma+r%C3%A9ponse+est+traduite</string>< / Texts></Texts>< To> de< / To><To>de</To> < / TranslateArrayRequest></TranslateArrayRequest>这转换为:Ma Ré ponse istü bersetztThis translates to : Ma Réponse ist übersetzt WTF ?????? 在一个函数中,它成功解码,而另一个函数则没有。 WTF??????  In one function it successfully decodes in the other it does not. 现在,我尝试对重音字符进行URLE编码,以便用"%C3%A9"序列替换带有重音的e并运行它。在这样做的时候,我正在为字符串传递这个XML:< string xmlns =" http://schemas.microsoft.com/2003/10/Serialization/Arrays"> Ma r%C3% A9ponse est traduite< / string>Now, I try just URLEncoding the accented characters so I replace my accented e with the '%C3%A9' sequence and run it. In doing so, I'm effectively passing this XML for the string: <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Ma r%C3%A9ponse est traduite</string>翻译回来:"Meine%C3%A9ponse R wirdü bersetzt"The translation comes back: "Meine % C3 % A9ponse R wird übersetzt"用于重音字符,我应该将XML有效负载放入TranslateArray中以获得重音字符?如果我只输入高位ascii,MS翻译器总是以500响应代码响应并且没有其他信息。For accented characters, what should I put into the XML payload to TranslateArray for accented characters? If I put in just the high ascii, MS translator ALWAYS responds with a 500 response code and no additional information.这是HTTP API中的错误吗?Is this a bug in the HTTP API? -Hugh Ferguson-Hugh Ferguson 推荐答案 实际上如果你不用UTF-8对输入进行编码,那么erver会给出错误代码500.当然如果你对文本进行URLEncode,那么HTTP POST API的也是错误的。当您在URL中输入时,它应该用于HTTP GET。  如果你这样做,服务器不会爆炸,但会发出错误的翻译。   以下是使用UTF-8对字符串进行编码的示例,并且 服务器返回正确的结果。突出显示的文本是编码。请记住在运行之前设置正确的appid。     包 com.colin.test;   import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException ;importjava.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URLEncoder ;importjava.net.URLEncoder; import java.net.URL;   public class 译者{       public static void main(String [] args){           String appid = "你的appid需要在这里设置" ;           String translateArrayRequest = "< TranslateArrayRequest>"                 + "< AppId>" + appid + "< / AppId>"                 + "< From> fr< / From>"                 + "<文本>"                 + "< string xmlns = \" http://schemas.microsoft.com/2003/10/Serialization/Arrays\ "> Ma ré ponse< / string>"                 + "< / Texts>"                 + "< To> de< / To>"                 + "< / TranslateArrayRequest>" ;           String translateArrayUri =" http:/ /api.microsofttranslator.com/V2/http.svc/TranslateArray " ;;                // make TranslateArray HTTP POST方法调用         尝试 {               sendURL (translateArrayUri,translateArrayRequest, " POST" );           sendURL(translateArrayUri, translateArrayRequest, "POST");        } catch (例外e){             System。 out 。println( " exception" );             System。 out 。println(e.getMessage());        }      }     private static void sendURL(String theURL,String theArgs,String method) throws 异常{         URL myURL = new URL(theURL);         HttpURLConnection httpConn =(HttpURLConnection)myURL.openConnection();           //给它15秒响应         httpConn.setReadTimeout(15 * 1000);           httpConn.setRequestMethod(method);         httpConn.setRequestProperty( "主机" , " api.microsofttranslator.com" );         httpConn.setRequestProperty( " User-Agent" , "" );         httpConn.setRequestProperty( "内容类型" , " text / xml" );           if (method.equalsIgnoreCase( " POST" )) {             httpConn.setRequestProperty( " Content-Length" ,整数。 toString ( theArgs.getBytes( " UTF-8" )。 长度 ));             httpConn.setRequestProperty( " Accept-Language" , " en_US,en" );             httpConn.setDoOutput( true );        }         httpConn.setDoInput( true );           //发送请求         if (method.equalsIgnoreCase( " POST" )) {             httpConn.getOutputStream()。write( theArgs.getBytes(" UTF-8"));        }                   //读取服务器的输出         BufferedReader reader;         String line = null ;          System。 out 。println( "响应代码:" ; + httpConn.getResponseCode());         if (httpConn.getResponseCode()!= 200){             reader = new BufferedReader( 新 InputStreamReader(httpConn.getErrorStream()));        } else {             reader = new BufferedReader( 新 InputStreamReader(httpConn.getInputStream()));        }           while ((line = reader.readLine())!= null )         {             System。 out 。println(line);        }                  reader.close();           if (httpConn!= null )             httpConn.disconnect();    }   } 这篇关于HTTP API:为什么Translate和TranslateArray会返回不同的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 03:08