【问题标题】:400: bad request400:错误请求
【发布时间】:2013-03-26 11:44:13
【问题描述】:

我正在使用 csmanage 访问 Azure 管理 API。这是我的代码:

    private const string subscriberID = "<id>";

    static void Main(string[] args)
    {
        // The thumbprint value of the management certificate.
        // You must replace the string with the thumbprint of a 
        // management certificate associated with your subscription.
        string certThumbprint = "<thumbprint>";

        // Create a reference to the My certificate store.
        X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);

        // Try to open the store.
        try
        {
            certStore.Open(OpenFlags.ReadOnly);
        }
        catch (Exception e)
        {
            throw;
        }

        // Find the certificate that matches the thumbprint.
        X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, certThumbprint, false);
        certStore.Close();

        // Check to see if our certificate was added to the collection. If no, throw an error, if yes, create a certificate using it.
        if (0 == certCollection.Count)
        {
            throw new Exception("Error: No certificate found containing thumbprint " + certThumbprint);
        }

        // Create an X509Certificate2 object using our matching certificate.
        X509Certificate2 certificate = certCollection[0];



        var serviceManagment = ServiceManagementHelper.CreateServiceManagementChannel("WindowsAzureEndPoint", new X509Certificate2(certificate));
        var x = serviceManagment.ListHostedServices(subscriberID);

        foreach (HostedService s in x)
        {
            Console.WriteLine(s.ServiceName);
        }
    }

这在控制台应用程序中运行良好。但是,当我在 WCF 项目(作为服务实现)中执行完全相同的代码时,我得到了400 - Bad Request

什么可能导致这个错误?

【问题讨论】:

  • 尝试使用 fiddler 跟踪请求和响应 - 来自控制台应用程序和表单 WCF 服务应用程序,检查请求的差异。
  • 我已经用过 fiddler。根本找不到任何区别..
  • 那么反应如何? fiddler 中的 400 响应详细信息应该更具体一些(有时它们是..)如果您为这两个请求粘贴请求/响应对,我们可能会提供更多帮助。使用 fiddler 中的Raw 部分从请求和响应中获取原始数据。

标签: c# wcf azure


【解决方案1】:

不是真正的答案,但您可以做的一件事是通过使用类似于以下的代码捕获 Web 异常来查看有关 400 错误的更多详细信息:

    catch (WebException webEx)
    {
        string errorDetail = string.Empty;
        using (StreamReader streamReader = new StreamReader(webEx.Response.GetResponseStream(), true))
        {
            errorDetail = streamReader.ReadToEnd();
        }
    }

这里的errorDetail 将是一个 XML,它应该为您提供更多信息。

【讨论】:

  • 是的,Azure 管理服务倾向于提供一些关于各种错误的数据,这些数据只能通过 (1) Fiddler 或 (2) Gaurav 解释的方法看到。
  • 遍历 InnerException。在某个地方,您会发现 WebException 类型的异常。
  • 确实有一个WebException,但是里面除了“Bad Request”之外没有更多的信息.​​..
  • 首先,我能够重现您面临的问题。不是真正的 WCF 专家,但我注意到一件奇怪的事情:由于某种原因,通过 WCF 服务发送到 Windows Azure 的请求是“POST”请求而不是“GET”请求。该请求通过控制台应用程序作为“GET”请求(这是正确的)发送。我相信你只是因为这个而得到这个错误。希望这能给你一些想法。
  • 谢谢,你是怎么找到的?有什么线索可以解决吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-21
  • 2018-04-07
  • 2019-11-05
相关资源
最近更新 更多