Appharbor JustOneDB https来自C#的请求获得400个错误

时间:2012-04-05 13:58:31

标签: c# https httpwebrequest appharbor justonedb

我正在尝试对JustOneDB执行https GET,如果我从curl实用程序执行此操作。但它不适用于C#。 我得到(400)错误请求

我四处搜索并禁用了安全性,但仍然没有工作。有任何想法吗? 有人做过这个w / rest和JustOneDB吗?

这与其他所有其他例子一起使用:

curl -k -XGET 'https://username:password@77.92.68.105:31415/justonedb/database/database name'

这不起作用:我愚弄了字符串以删除我的密码。

   public ActionResult JustOneDb()
    {
        ///////////
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        try
        {

            String Xml;
            //curl -k -XGET 'https://zn0lvkpdhdxb70l2_DUMMY_urshn5e7i41lb3fiwuh@77.92.68.105:31415/justonedb/database/n10lvkpdhdxei0l2uja'
            string url = @"https://zn0lvkpdhdxb70_DUMMY_urshn5e7i41lb3fiwuh@77.92.68.105:31415/justonedb/database/n10lvkpdhdxei0l2uja";

            request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "GET";

            request.Credentials = CredentialCache.DefaultCredentials;

            // Ignore Certificate validation failures (aka untrusted certificate + certificate chains)
            ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); 


            // Get response 
            using (response = (HttpWebResponse)request.GetResponse())
            {
                // Get the response stream 
                StreamReader reader = new StreamReader(response.GetResponseStream());
                Xml = reader.ReadToEnd();
            }
            return Content(Xml);
        }
        catch (Exception ee)
        {
            return Content(ee.ToString());

        }
        //////////////

        ViewBag.fn = "*.xml";
        return View();
    }

结果:

System.Net.WebException: The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.GetResponse() 
at Mvc3Razor.Controllers.MyXmlController.JustOneDb() ...

TIA FxM:{

3 个答案:

答案 0 :(得分:1)

您应该使用“username”和“password”创建正确的凭据,而不是在网址中传递用户名密码。您可能还需要考虑使用RestSharp之类的内容而不是原始WebRequest

答案 1 :(得分:0)

与证书不匹配,已修复。它仍然是自签名的,但现在主机名匹配。我们将尽快更改为使用完全签名的证书,但同时请告诉我它是否仅适用于自签名覆盖。

答案 2 :(得分:0)

好的,我复制了代码,问题是HttpWebRequest库没有在原始请求中添加身份验证标头。解决方案是手动插入标题,因此如果在'request.Method =“GET”行之后添加以下两行(并从URL字符串中删除用户名/密码):

string authInfo = "zn0lvkpdhdxb70l2_DUMMY_urshn5e7i41lb3fiwuh";
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));

它应该可以正常工作。