简体   繁体   English

C# HTTP 发布 Curl 问题

[英]C# HTTP Post Curl issue

I am trying to post a specific HTTP Post however i am not understanding the conversion for C# to do this correctly, I feel like I am most of the way there but getting stuck along the way我正在尝试发布特定的 HTTP 帖子,但是我不理解 C# 的转换是否正确,我觉得我大部分时间都在那里,但一路上卡住了

This is what I am trying to send:这就是我要发送的内容:

curl -i -X POST -H 'Content-Type: application/json' -d '{"text": " Some text or a string in my case a string :tada:"}' http://example.com/hooks/KEYDATA

Should look something like this...应该是这个样子...

POST /hooks/KEYDATA HTTP/1.1
Host: http://example.com
Content-Type: application/json
Content-Length: 63

Here is what I have....这就是我所拥有的......

           async Task sendRequest()
            {
                using var httpClient = new HttpClient();
                using var request = new HttpRequestMessage(new HttpMethod("POST"), 
"https://example.com/hooks/KEYDETAIL");
                request.Headers.TryAddWithoutValidation("Content-Type", "application/json");
                request.Content = new StringContent("{\"text\":\" " + questionCreated + "\" :tada:\"}", 
Encoding.UTF8, "application/json");

                var response = await httpClient.SendAsync(request);
                MessageBox.Show(request.ToString());
            }

You are trying to display the request instead of the response.您正在尝试显示请求而不是响应。

The response is of type HttpResponseMessage响应的类型为HttpResponseMessage

You are displaying the request in the message box, not the response.您在消息框中显示请求,而不是响应。 Assuming you want to see the actual response stream, try the following, it reads the content as string:假设您想查看实际响应 stream,请尝试以下操作,它将内容读取为字符串:

MessageBox.Show(await response.Content.ReadAsStringAsync());

See also: https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent?view=netcore-3.1另请参阅: https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent?view=netcore-3.1

With the HttpResponseMessage you're able to read multiple properties like the headers or status code.使用 HttpResponseMessage,您可以读取多个属性,例如标题或状态代码。


In order to debug such things, try to read the responses content and statuscode properties.为了调试这些东西,请尝试读取响应内容和状态码属性。

Another tip: I noticed you mannualy constructing a JSON string;另一个提示:我注意到您手动构建了 JSON 字符串; using NewtonSoft you should be able to do just this:使用 NewtonSoft 你应该能够做到这一点:

string jsonstring = JsonConvert.SerializeObject(new { text = "tada" } ); 

Another tip, for me usualy the following suffies:另一个提示,对我来说,通常以下内容就足够了:

using (var client = new HttpClient())
{
    //no need to set header
    var request = new HttpRequestMessage(new HttpMethod("POST"), "https://example.com/hooks/KEYDETAIL")
    {
        Content = new StringContent(JsonConvert.SerializeObject(yourobject),
                                    Encoding.UTF8, "application/json")
    };

     var response = await httpClient.SendAsync(request);
} 

Your C# code generates a different/invalid json string than you provided in the curl call.您的 C# 代码生成的 json 字符串与您在 curl 调用中提供的字符串不同/无效。

If you take如果你拿

request.Content = new StringContent("{\"text\":\" " + questionCreated + "\" :tada:\"}", 
    Encoding.UTF8, "application/json");

The resulting string will be (look at the additional " before :tata: ).结果字符串将是(查看:tata:之前的附加" )。

{"text":" Some text or a string in my case a string" :tada:"}

instead of代替

{"text": " Some text or a string in my case a string :tada:"}

Replace it with将其替换为

request.Content = new StringContent("{\"text\":\" " + questionCreated + " :tada:\"}", 
    Encoding.UTF8, "application/json");

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

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