将cURL转换为HttpClient.PutAsync

时间:2016-09-30 19:35:30

标签: c# rest redirect httpclient

我正在尝试将一些cURL命令转换为C#实现,但我遇到了一个我无法解决的异常。

我试图收集尽可能多的信息,希望有人可以帮助我。这就是......

cURL声明:

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

C#版本:

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

C#版本会生成WebException

  

HResult = -2146233088

     

消息=无法解析远程名称:'ip-172-31-9-79.eu-central-1.compute.internal'

我连接的服务器是Hadoop服务器。该服务器上运行了多个实例。因此,当我存储文件时,服务器会回复有关存储此文件的实例的信息(以便在我想写入此文件时可以引用该实例)。

基于错误消息,似乎它接收到某种无法访问的IP地址的引用(这是有道理的,因为该IP地址是该Hadoop服务器中的内部IP地址。

我使用Wireshark查找发送请求的差异。

使用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]

使用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]

对我来说,唯一值得注意的(但对我来说毫无意义)差异是:

  • User-Agent:curl / 7.50.0 \ r \ n
  • 接受: / \ r \ n

VS

  • 内容长度:0 \ r \ n
  • 连接:Keep-Alive \ r \ n

我对REST编程的经验很少,显然对分析Web请求知之甚少。一些帮助/指导/解释将不胜感激。

为了完整性:

  • 是的我知道有cURL的Windows可执行文件,但我在Raspberry PI / Windows IoT上运行。据我所知,那个特定的平台(尚未)
  • 在上述陈述中,我故意遗漏了ipaddress / appname / etc,以确保安全方面的安全问题

1 个答案:

答案 0 :(得分:1)

我发现唯一出错的是在C#中读取响应。通过Wireshark监视的HTTP响应是相同的(使用cURL.exe并在C#中使用PutAsync)。

我能够忽略重定向,并有机会自己处理响应。

因此,禁用自动重定向(并自行处理响应)解决了我的问题:

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