本文介绍了400坏请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


获得以下内容。

getting the following.

HTTP/1.1 400 Bad Request

Content-Length: 264

Content-Type: text/html; charset=utf-8

X-MS-Trans-Info: s=35641

Date: Wed, 25 Jun 2014 14:19:55 GMT

<html><body><h1>TranslateApiException</h1><p>Method: Translate()</p><p>Message: The Azure Market Place Translator Subscription associated with the request credentials has zero balance.</p><code></code><p>message id=5641.V2_Rest.Translate.40BD29BC</p></body></html>

我正在使用我的应用程序中的客户端ID和密码订阅标记为活动。我的代码如下。

I am using the client id and secret from my application and I have a subscription marked active. my code is below.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace transalte
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Token toke = Task.Run(async () =>
                {
                    HttpClient cl = new HttpClient();

                    var token = new HttpRequestMessage();
                    Uri url = new Uri("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13");

                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    dic.Add("grant_type", "client_credentials");
                    dic.Add("client_id", "CLIENTID");
                    dic.Add("client_secret", "SECRET");
                    dic.Add("scope", "http://api.microsofttranslator.com");

                    var r2 = await cl.PostAsync(url, new FormUrlEncodedContent(dic));

                    var c = await r2.Content.ReadAsStringAsync();
                    Token o = JsonConvert.DeserializeObject<Token>(c);
                    return o;
                }).ConfigureAwait(false).GetAwaiter().GetResult();

            string text = Console.ReadLine();

            while (text != "q")
            {
                Task.Run(async () =>
                    {
                        using (HttpClient cl = new HttpClient())
                        {
                            using (var r = new HttpRequestMessage())
                            {
                               
                                r.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", toke.access_token);
                                r.RequestUri = new Uri("http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + text + "&to=fr");

                                var la = await cl.SendAsync(r);

                                var resp = XDocument.Parse(await la.Content.ReadAsStringAsync());

                                Console.WriteLine(resp.Root.Value);
                            }
                        }
                    }).ConfigureAwait(false);
                text = Console.ReadLine();
            }
        }

        private class Token
        {
            public string access_token { get; set; }

            public string expires_in { get; set; }

            public string scope { get; set; }

            public string token_type { get; set; }
        }
    }
}

推荐答案


这篇关于400坏请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 20:51