WebRequest equivalent of `curl -X DELETE ...`



我在创建一个相当于以下curl命令的WebRequest时遇到了麻烦:

curl -vX DELETE "http://admin:123@localhost:5984/booster"

命令工作正常,给出如下输出:

C:ProjectsBoosterBin>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)方法不允许。"

服务器响应"{"error";method_not_allowed";reason";Only GET,HEAD allowed"}"

<标题>:

curl命令和通过WebRequest执行的命令有什么区别?为什么第一种情况下一切顺利,第二种情况下一切都失败了?

我已经设法找出了问题所在,所以下面是:

Wireshark显示了客户端和服务器之间的通信:

WebRequest案例

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不使用Basic授权,而curl使用Basic授权。在谷歌上搜索了一下,显示了在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());
}

最新更新