简体   繁体   English

将 cURL 转换为 HttpClient.PutAsync

[英]Translating cURL to HttpClient.PutAsync

I am trying to get a few cURL commands translated to a C# implementation, but I am running into an Exception which I can't solve.我正在尝试将一些 cURL 命令转换为 C# 实现,但我遇到了无法解决的异常。

I tried to gather up as much information as I can in the hopes that somebody can help me further.我试图收集尽可能多的信息,希望有人能进一步帮助我。 Here it goes...来了...

The cURL statement: cURL 语句:

curl -i -X PUT "http://[ipaddress]:[port]/webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=[username]&op=CREATE"

The C# version: C# 版本:

var response = await client.PutAsync(
    "http://[ipaddress]:[port]/webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=[username]&op=CREATE",
    null);

The C# version results in a WebException : C# 版本导致WebException

HResult = -2146233088 HResult = -2146233088

Message = The remote name could not be resolved: 'ip-172-31-9-79.eu-central-1.compute.internal'消息 = 无法解析远程名称:'ip-172-31-9-79.eu-central-1.compute.internal'

The server I am connecting to, is a Hadoop server.我要连接的服务器是 Hadoop 服务器。 There are multiple instances running on that server.该服务器上运行着多个实例。 So when I store a file, the server replies with information on which instance this file is stored (so that I can reference that instance when I want to write to this file).因此,当我存储文件时,服务器会回复有关存储该文件的实例的信息(以便在我想写入该文件时可以引用该实例)。

Based on the error message, it seems to be that it receives some kind of reference to an IP address it can't access (which makes sense, as that ip address is an internal IP address in that Hadoop server.根据错误消息,它似乎收到了对其无法访问的 IP 地址的某种引用(这是有道理的,因为该 IP 地址是该 Hadoop 服务器中的内部 IP 地址。

I used Wireshark to find the difference in the requests which are sent.我使用 Wireshark 来查找发送的请求之间的差异。

Using cURL:使用卷曲:

Frame 57: 204 bytes on wire (1632 bits), 204 bytes captured (1632 bits) on interface 0
Ethernet II, Src: IntelCor_da:f4:44 (fc:f8:ae:da:f4:44), Dst: AsustekC_32:7d:b0 (ac:22:0b:32:7d:b0)
Internet Protocol Version 4, Src: 192.168.1.107, Dst: [ipaddress]
Transmission Control Protocol, Src Port: 60454, Dst Port: 50070, Seq: 1, Ack: 1, Len: 150
Hypertext Transfer Protocol
    PUT /webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE HTTP/1.1\r\n
        [Expert Info (Chat/Sequence): PUT /webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE HTTP/1.1\r\n]
            [PUT /webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE HTTP/1.1\r\n]
            [Severity level: Chat]
            [Group: Sequence]
        Request Method: PUT
        Request URI: /webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE
            Request URI Path: /webhdfs/v1/[appname]/staging/sensors/aap.txt
            Request URI Query: user.name=hdfs&op=CREATE
                Request URI Query Parameter: user.name=hdfs
                Request URI Query Parameter: op=CREATE
        Request Version: HTTP/1.1
    Host: [ipaddress]:50070\r\n
    User-Agent: curl/7.50.0\r\n
    Accept: */*\r\n
    \r\n
    [Full request URI: http://[ipaddress]:50070/webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE]
    [HTTP request 1/1]
    [Response in frame: 59]

Using the HttpClient :使用HttpClient

Frame 381: 209 bytes on wire (1672 bits), 209 bytes captured (1672 bits) on interface 0
Ethernet II, Src: IntelCor_da:f4:44 (fc:f8:ae:da:f4:44), Dst: AsustekC_32:7d:b0 (ac:22:0b:32:7d:b0)
Internet Protocol Version 4, Src: 192.168.1.107, Dst: [ipaddress]
Transmission Control Protocol, Src Port: 60541, Dst Port: 50070, Seq: 1, Ack: 1, Len: 155
Hypertext Transfer Protocol
    PUT /webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE HTTP/1.1\r\n
        [Expert Info (Chat/Sequence): PUT /webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE HTTP/1.1\r\n]
            [PUT /webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE HTTP/1.1\r\n]
            [Severity level: Chat]
            [Group: Sequence]
        Request Method: PUT
        Request URI: /webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE
            Request URI Path: /webhdfs/v1/[appname]/staging/sensors/aap.txt
            Request URI Query: user.name=hdfs&op=CREATE
                Request URI Query Parameter: user.name=hdfs
                Request URI Query Parameter: op=CREATE
        Request Version: HTTP/1.1
    Host: [ipaddress]:50070\r\n
    Content-Length: 0\r\n
    Connection: Keep-Alive\r\n
    \r\n
    [Full request URI: http://[ipaddress]:50070/webhdfs/v1/[appname]/staging/sensors/aap.txt?user.name=hdfs&op=CREATE]
    [HTTP request 1/1]
    [Response in frame: 383]

To me, the only notable (but for me meaningless) differences are:对我来说,唯一显着(但对我来说毫无意义)的差异是:

  • User-Agent: curl/7.50.0\\r\\n用户代理: curl/7.50.0\\r\\n
  • Accept: / \\r\\n接受: / \\r\\n

vs对比

  • Content-Length: 0\\r\\n内容长度: 0\\r\\n
  • Connection: Keep-Alive\\r\\n连接: 保持活动\\r\\n

I have very little experience with REST programming, and clearly not enough knowledge on analyzing web requests.我对 REST 编程的经验很少,而且显然没有足够的知识来分析 Web 请求。 Some help / guidance / explanation would be greatly appreciated.一些帮助/指导/解释将不胜感激。

Just for completeness:只是为了完整性:

  • Yes I am aware that there are Windows executables for cURL, but I run on Raspberry PI / Windows IoT.是的,我知道 cURL 有 Windows 可执行文件,但我在 Raspberry PI/Windows IoT 上运行。 As far as I know there is nothing for that specific platform (yet)据我所知,那个特定平台没有任何东西(还)
  • In the above statements I left out ipaddress/appname/etc intentionally just to be safe side wrt security issues在上面的陈述中,我故意省略了 ipaddress/appname/etc 只是为了安全方面的安全问题

I found out that the only thing which goes wrong is reading the response in C#.我发现唯一出错的是在 C# 中读取响应。 The HTTP responses monitored over Wireshark are identical (using cURL.exe and using PutAsync in C#).通过 Wireshark 监控的 HTTP 响应是相同的(在 C# 中使用 cURL.exe 和 PutAsync)。

I was able to ignore the redirect and be given the opportunity to handle the response myself.我能够忽略重定向并有机会自己处理响应。

So disabling auto redirect (and handle the response myself) solved my problem:因此禁用自动重定向(并自己处理响应)解决了我的问题:

var httpClientHandler = new HttpClientHandler {AllowAutoRedirect = false};
var client = new HttpClient(httpClientHandler);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM