【发布时间】:2019-11-30 01:43:52
【问题描述】:
我正在尝试采用以下 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();
}
有什么想法吗?
【问题讨论】:
-
尝试向 Web 服务器发出请求,该服务器只会吐出您发送的内容(如 httpbin.org,或自己创建这样的服务器)并检查差异。
-
可能需要数据来发送每个请求的更改。
标签: c# api curl httpwebrequest ip-address