开发者

Trying to access a Microsoft web-service causes a 400 Bad Request error

I'm trying to use the microsoft translator webservice in an application. Though when I try to get language names according to the example given at http://msdn.microsoft.com/en-us/library/ff512414.aspx, I get a (400) Bad Request error when I actually try to get the language names.

The URI I use is:

private const string languageString = "http://api.microsofttranslator.com/v2/Http.svc/GetLanguageNames";
private string[] usableLanguages = { "en", "nl", "es", "de" };

// Create a URI to use.
string getLanguages = string.Empty;
getLanguages = languageString + string.Format("?appId={0}&locale;=en", appKey);
Uri langRequest = new Uri(getLanguages);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(langRequest);

After that I use a DataContractSerializer and the stream from the WebRequest to send the request to the web service. But when I try to get the response from the WebRequest, I get the (400) Bad Request error.

WebResponse response = null;
try
{
    // This causes the exception.
    response = req.GetResponse();
}
catch (WebException e)
{
    MessageBox.Show(e.ToString());
}
final开发者_Python百科ly
{
    if (response != null)
    {
        response.Close();
        response = null;
    }
}

If anybody has an idea, please tell me.


After some further development, I now know that the error lies with the locale I pass along the URI. After further dissecting of the WebException, I get the status code ProtocolError, and the message "Message: 'locale' must be a valid language". However, I thought 'en' was a valid language... :/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜