我试图从.Net MVC应用程序连接到Cloudant(沙发式数据库).我遵循使用HttpClient使用Web API的准则,如下所示:http: //www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-一个网客户端
到目前为止,我有两种方法 - 一种是获取文档,另一种是创建文档 - 两者都有错误.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;
}
Run Code Online (Sandbox Code Playgroud)
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;
} …Run Code Online (Sandbox Code Playgroud)