为什么cURL有效,但我的HttpWebRequest不起作用?

时间:2019-11-30 01:43:52

标签: c# api curl httpwebrequest ip-address

我正在尝试采用以下curl语句,并以c#方法重新执行它。以下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/

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"
   }
}

但是,当我尝试在C#中重新执行以上操作时,它却给我带来了令人恐惧的“我不告诉你为什么为什么不工作”消息:“远程服务器返回错误:(500)内部服务器错误。”

这是我的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();
            }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

对发现我的问题的fenixil表示敬意:

只需要使用Base64用户名/密码对即可。现在像冠军一样!