c# - WebRequest 相当于 `curl -X DELETE ...`

标签 c# .net curl couchdb

我在创建与以下 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) 方法不允许。”

服务器响应 '{"error":"method_not_allowed","re​​ason":"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"}

curl 案例

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 确实使用了一个。一点谷歌搜索显示了在 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());
}

关于c# - WebRequest 相当于 `curl -X DELETE ...`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8263993/

相关文章:

tomcat - 使用 cURL 的 j_security 身份验证

macos - MacOS 上的 DotNet core 2.0 在使用 couchbase dotnet sdk 时获取由 httpclient 触发的 libcurl 和 ssl 错误

c# - Web.config 中的程序集

c# - .Net Core Web API 方法是否与 EF Core 查询异步

.net - 更改多个项目的程序集版本

c# - 创建自定义异常还是使用内置异常?

linux - 如何在环境变量扩展中使用引号

c# - 我的 WPF ListView 不会绑定(bind)

c# - 解析从美国时区到日期时间的日期时间。

c# - 如何使用 OpenXML SDK 将 Excel 转换为 CSV?