C# 400:请求错误
我正在使用csmanage访问Azure管理API。这是我的代码:C# 400:请求错误,c#,wcf,azure,C#,Wcf,Azure,我正在使用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 thumbpri
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);
}
}
private const string subscriberID=“”;
静态void Main(字符串[]参数)
{
//管理证书的指纹值。
//必须将字符串替换为
//与您的订阅关联的管理证书。
字符串certThumbprint=“”;
//创建对“我的证书存储”的引用。
X509Store certStore=新的X509Store(StoreName.My,StoreLocation.CurrentUser);
//试着开一家店。
尝试
{
打开(OpenFlags.ReadOnly);
}
捕获(例外e)
{
投掷;
}
//查找与指纹匹配的证书。
X509Certificate2Collection certCollection=certStore.Certificates.Find(X509FindType.FindByThumbprint,certThumbprint,false);
certStore.Close();
//检查证书是否已添加到集合中。如果否,则抛出错误;如果是,则使用它创建证书。
if(0==certCollection.Count)
{
抛出新异常(“错误:未找到包含指纹的证书”+certThumbprint);
}
//使用匹配的证书创建X509Certificate2对象。
X509Certificate2 certificate=certCollection[0];
var serviceManagment=ServiceManagementHelper.CreateServiceManagementChannel(“WindowsAzureEndPoint”,新的X509Certificate2(证书));
var x=ServiceManager.ListHostedServices(subscriberID);
foreach(x中的HostedService s)
{
Console.WriteLine(s.ServiceName);
}
}
这在控制台应用程序中运行良好。然而,当我在一个WCF项目中执行完全相同的代码(作为服务实现)时,我得到的结果是400-错误的请求
什么可能导致此错误?这不是一个真正的答案,但您可以做的一件事是通过使用类似以下代码捕获web异常来查看有关400错误的更多详细信息:
catch (WebException webEx)
{
string errorDetail = string.Empty;
using (StreamReader streamReader = new StreamReader(webEx.Response.GetResponseStream(), true))
{
errorDetail = streamReader.ReadToEnd();
}
}
这里的errorDetail
将是一个XML,可以为您提供更多信息。尝试使用fiddler跟踪请求和响应-从控制台应用程序和表单WCF服务应用程序,检查请求的差异。我已经使用了fiddler。根本找不到任何差异..反应如何?如果您为两个请求粘贴请求/响应对,fiddler中的400响应详细信息将更加具体(有时是…),我们可能能够提供更多帮助。使用fiddler中的Raw
部分从request and response.yep获取原始数据,Azure管理服务倾向于提供有关各种错误的一些数据,这些数据只能由(1)fiddler或(2)Gaurav解释的方法看到。遍历InnerException。在下面的某个地方,你会发现一个WebException类型的异常。确实有一个WebException,但除了“坏请求”之外,没有其他信息了……首先,我能够重现你面临的问题。不是一个真正的WCF专家,但我注意到一件奇怪的事情:出于某种原因,发送到WindowsAzure的请求在通过WCF服务发送时是一个“POST”请求,而不是一个“GET”请求。请求通过控制台应用程序以“获取”请求的形式发送(这是正确的)。我相信你犯这个错误只是因为这个。希望这能给你一些想法。谢谢,你觉得怎么样?有什么线索可以帮我解决吗?