加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用.Net HttpClient访问cloudant数据库

发布时间:2020-12-16 01:55:19 所属栏目:百科 来源:网络整理
导读:我试图从.Net MVC应用程序连接到Cloudant(沙发式数据库).我遵循使用HttpClient使用Web API的准则,如下所示: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client 到目前为止,我有两种方法 – 一种是获取文档,另一种
我试图从.Net MVC应用程序连接到Cloudant(沙发式数据库).我遵循使用HttpClient使用Web API的准则,如下所示:
http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

到目前为止,我有两种方法 – 一种是获取文档,另一种是创建文档 – 两种方法都有错误. Get方法返回Unauthorized,Post方法返回MethodNotAllowed.

客户端创建如下:

private HttpClient CreateLdstnCouchClient()
    {
        // TODO: Consider using WebRequestHandler to set properties


        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(_couchUrl);

        // Accept JSON
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));


        return client;
    }

Get方法是:

public override string GetDocumentJson(string id)
    {
        string url = "/" + id;

        HttpResponseMessage response = new HttpResponseMessage();
        string strContent = "";

        using (var client = CreateLdstnCouchClient())
        {
            response = client.GetAsync(url).Result;

            if (response.IsSuccessStatusCode)
            {
                strContent = response.Content.ReadAsStringAsync().Result;
            }
            else
            {
                // DEBUG
                strContent = response.StatusCode.ToString();
                LslTrace.Write("Failed to get data from couch");
            }
        }

        return strContent;
    }

Post方法是:

public override string CreateDocument(object serializableObject)
    {
        string url = CouchApi.CREATE_DOCUMENT_POST;

        HttpResponseMessage response = new HttpResponseMessage();

        string strContent = "";

        using (var client = CreateLdstnCouchClient())
        {

            response = client.PostAsJsonAsync(url,serializableObject).Result;
            strContent = response.Content.ReadAsStringAsync().Result;
        }

        if (response.IsSuccessStatusCode)
        {
            return strContent;
        }
        else
        {
            LslTrace.Write("{0} ({1})",(int)response.StatusCode,response.ReasonPhrase);
            return response.StatusCode.ToString();
        }
    }

URL根据API文档:https://username:password@username.cloudant.com.

我对发生的事情非常困惑,并且在查找示例时遇到了很多麻烦.谢谢你的帮助!

托马斯

解决方法

使用HttpClient,您需要执行以下操作以正确进行身份验证(假设您使用基本身份验证):

HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(_userName,_password);
HttpClient client = new HttpClient(handler) {
    BaseAddress = new Uri(_couchUrl)
};

您不应在_couchUrl中指定用户名/密码 – HttpClient不支持该用户名/密码.

我看不到您的PostAsJsonAsync的实现或您正在构建的完整Url,但是您可以尝试检查/记录response.ReasonPhrase,当出现错误时,可以获得有关出错的提示.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读