我在创建与以下curl命令相当的WebRequest时遇到问题:
curl -vX DELETE "http://admin:123@localhost:5984/booster"
该命令工作正常,给出以下输出:
C:\Projects\Booster\Bin>curl -vX DELETE "http://admin:123@localhost:5984/booster"
* About to connect() to localhost port 5984 (#0)
* Trying 127.0.0.1... connected
* Server auth using Basic with user 'admin'
> DELETE /booster HTTP/1.1
> Authorization: Basic YWRtaW46MTIz
> User-Agent: curl/7.22.0 (i386-pc-win32) libcurl/7.22.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:5984
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: CouchDB/1.0.2 (Erlang OTP/R14B)
< Date: Fri, 25 Nov 2011 01:03:45 GMT
< Content-Type: text/plain;charset=utf-8
< Content-Length: 12
< Cache-Control: must-revalidate
<
{"ok":true}
* Connection #0 to host localhost left intact
* Closing connection #0
并按预期删除CouchDB数据库,但是当我使用以下等效代码时:
try
{
var request = WebRequest.Create("http://admin:123@localhost:5984/booster");
request.Headers.Clear();
request.Method = "DELETE";
var response = request.GetResponse();
Console.WriteLine(response.GetResponseString());
}
catch (WebException e)
{
Console.WriteLine(e.Response.GetResponseString());
}
为了和curl命令一样,我得到一个异常告诉
“远程服务器返回错误:(405)Method Not Allowed。”
服务器响应'{“错误”:“method_not_allowed”,“reason”:“只允许GET,HEAD允许”}'
curl命令与通过WebRequest执行的命令有什么区别?为什么在第一种情况下一切正常,在第二种情况下一切都失败了?
答案 0 :(得分:3)
我已经设法弄清楚问题是什么,所以这里有:
Wireshark显示了客户端和服务器之间的以下通信:
DELETE /booster HTTP/1.1
Host: localhost:5984
Connection: Keep-Alive
HTTP/1.1 302 Moved Temporarily
Server: CouchDB/1.0.2 (Erlang OTP/R14B)
Location: http://localhost:5984/_utils/session.html?return=%2Fbooster&reason=You%20are%20not%20a%20server%20admin.
Date: Fri, 25 Nov 2011 09:54:29 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 64
Cache-Control: must-revalidate
{"error":"unauthorized","reason":"You are not a server admin."}
DELETE /_utils/session.html?return=%2Fbooster&reason=You%20are%20not%20a%20server%20admin. HTTP/1.1
Host: localhost:5984
HTTP/1.1 405 Method Not Allowed
Server: CouchDB/1.0.2 (Erlang OTP/R14B)
Date: Fri, 25 Nov 2011 09:54:29 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 64
Cache-Control: must-revalidate
Allow: GET,HEAD
{"error":"method_not_allowed","reason":"Only GET,HEAD allowed"}
DELETE /booster HTTP/1.1
Authorization: Basic YWRtaW46MTIz
User-Agent: curl/7.22.0 (i386-pc-win32) libcurl/7.22.0 OpenSSL/0.9.8r zlib/1.2.5
Host: localhost:5984
Accept: */*
HTTP/1.1 404 Object Not Found
Server: CouchDB/1.0.2 (Erlang OTP/R14B)
Date: Fri, 25 Nov 2011 09:54:14 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 41
Cache-Control: must-revalidate
{"error":"not_found","reason":"missing"}
它表明WebRequest不使用基本授权,而curl确实使用了一个。一点点谷歌搜索显示了在WebRequest上使用基本授权的方式,它看起来如下:
try
{
var request = WebRequest.Create("http://localhost:5984/booster");
request.Headers.Clear();
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("admin:123"));
request.Method = "DELETE";
var response = request.GetResponse();
Console.WriteLine(response.GetResponseString());
}
catch (WebException e)
{
Console.WriteLine(e.Response.GetResponseString());
}