简体   繁体   English

为什么 cURL 有效,但我的 HttpWebRequest 无效?

[英]Why does cURL work, but my HttpWebRequest does not?

I'm trying to take the below curl statement and reperform it in ac# method.我正在尝试采用以下 curl 语句并在 ac# 方法中重新执行它。 The following curl statement works fine (I masked the credentials and IP address) so I'm sure the destination server is willing to play ball:以下 curl 语句工作正常(我屏蔽了凭据和 IP 地址),因此我确定目标服务器愿意玩球:

curl -D- -u user:password -X POST --data @data.txt -H "Content-Type: application/json" http://0.0.0.0:8080/rest/api/2/issue/

where the data.txt looks like this: data.txt 如下所示:

{
    "fields": {
       "project":{"id": "10000"},
       "summary": "No REST for the Wicked.",
       "description": "Creating of an issue using ids for projects and issue types using the REST API",
       "issuetype": {"id": "10002"},
    "customfield_10115" : "3212.12",
    "customfield_10116" : "Client Name",
    "customfield_10117" : "Engagement Name",
    "customfield_10118" : "TEst",
    "customfield_10121" : "2019-11-30",
    "customfield_10120" : "Daily"
   }
}

However, when I try and re-perform the above in C#, it's giving me the dreaded I'm-Not-Telling-You-Why-This-Doesn't-Work message: "The remote server returned an error: (500) Internal Server Error."但是,当我尝试在 C# 中重新执行上述操作时,它给了我可怕的 I'm-Not-Telling-You-Why-This-Doesn't-Work 消息: “远程服务器返回了一个错误:(500 ) 内部服务器错误。”

Here's my C# Code.这是我的 C# 代码。

            Console.WriteLine("Start");
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://0.0.0.0:8080/rest/api/2/issue/");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            httpWebRequest.Headers.Add("Authorization", "Basic user:password");

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {   
                string json = "{\"fields\": {\"project\":{ \"id\": \"10000\"},\"summary\": \"No REST for the Wicked.\",\"description\": \"description text\",\"issuetype\": { \"id\": \"10002\"},\"customfield_10115\" : \"3212.12\",\"customfield_10116\" : \"Client Name\",\"customfield_10117\" : \"Engagement Name\",\"customfield_10118\" : \"Payroll\",\"customfield_10121\" : \"2019-11-30\",\"customfield_10120\" : \"Daily\"}}";
                streamWriter.Write(json);
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }

Any ideas?有任何想法吗?

Kudos to fenixil who spotted my issue:感谢 fenixil 发现了我的问题:

Just needed to Base64 the username/password pair.只需要 Base64 用户名/密码对。 Works like a champ now!现在像冠军一样工作!

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

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