HttpClient Post 带有 JToken 和字符串的 MultipartFormDataContent returns 404 错误
HttpClient Post MultipartFormDataContent with JToken and string returns 404 error
我需要 post 一个请求包含:一个定义为字符串的 xml 和一个 json。我在正确设置内容和处理 JToken 方面遇到问题。
Fiddler 中的原始请求如下所示:
内容类型:multipart/form-data;边界=------------------------acebdf13572468
用户代理:Fiddler
授权:基本QURNSU46QURNSU4=
内容长度:888
主机:localhost
----------------------------acebdf13572468
Content-Disposition:表单数据;名字="fieldNameHere";文件名="testfile2.txt"
内容类型:text/plain
这是第二个测试文件,用于 eB EC 插件测试环境中的文件管理测试。
--------------------------acebdf13572468
Content-Disposition:表单数据;姓名="fieldNameHere"
内容类型:application/json
{
"instance":{
"className": "File",
"schemaName": "EB",
"relationshipInstances": [
{
"direction": "backward",
"className": "DocumentFiles",
"schemaName": "EB",
"relatedInstance":{
"className": "Document",
"schemaName": "EB",
"instanceId": "4"
}
}
],
"properties":{
"Name": "testfile2.txt"
}
}
}
----------------------------acebdf13572468--
我有这个方法,但出现 404 错误:
public static string PostXMLStringAndJasonInOneRequest(int instanceId)
{
// Creating json that describes document with property 'name'
JObject documentToPost = CreateClassInstance("File", "eB", null, new KeyValuePair<string, object>("Name", "MyTestDocument.txt"));
JObject relationship = CreateRelationshipClassInstance("DocumentFiles", "eB", null, "backward");
// specifying document parent that is existing project with its id
JObject documentParent = CreateClassInstance("Document", "eB", instanceId.ToString());
JToken json = CreateJson(documentToPost, relationship, documentParent);
Uri uri = new Uri("http://localhost/wsg/v2.0/xxxx");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Basic QURNSU46QURNSU4=");
MultipartFormDataContent form = new MultipartFormDataContent();
// TO-DO: save the xml string contents here
HttpContent content = new ByteArrayContent(GetBytes("<node>this is a text</node>"));
// HttpContent content = new StringContent("<node>this is a text</node>");
content.Headers.Add("Content-Type", "application/xml");
form.Add(content);
// TO-DO Save json - not hard coded the value of json
string jsonText = @"{
""instance"": {
""className"": ""File"",
""schemaName"": ""EB"",
""relationshipInstances"": [
{
""direction"": ""backward"",
""className"": ""DocumentFiles"",
""schemaName"": ""EB"",
""relatedInstance"": {
""className"": ""Document"",
""schemaName"": ""EB"",
""instanceId"": ""120""
}
}
],
""properties"": {
""Name"": ""testfile2.xml""
}
}
}";
HttpContent jsonContent = new ByteArrayContent(GetBytes(jsonText));
jsonContent.Headers.Add("Content-Type", "application/json");
// TO-DO put json not jsonText
form.Add(jsonContent);
HttpResponseMessage response = client.PostAsync(uri, form).Result;
if (response.StatusCode != HttpStatusCode.Created)
{
return "error"; // handle error
}
return response.Content.ReadAsStringAsync().Result;
}
}
// To create multipartcontent
// 404 is an error in my url address
private static HttpContent CreateMultipartContent(JToken json, Stream file, string fileName)
{
MultipartContent content = new MultipartContent("form-data", Guid.NewGuid().ToString());
StringContent jsonPart = new StringContent(json.ToString());
jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
jsonPart.Headers.ContentType = new MediaTypeHeaderValue("application/json");
StreamContent filePart = new StreamContent(file);
filePart.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
filePart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
filePart.Headers.ContentDisposition.FileName = fileName;
content.Add(jsonPart);
content.Add(filePart);
return content;
}
private static HttpContent CreateMultipartContent(JToken json, string markupText, string fileName)
{
MultipartContent content = new MultipartContent("form-data", Guid.NewGuid().ToString());
StringContent jsonPart = new StringContent(json.ToString());
jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
jsonPart.Headers.ContentType = new MediaTypeHeaderValue("application/json");
StringContent filePart = new StringContent(markupText);
filePart.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
filePart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
filePar`enter code here`t.Headers.ContentDisposition.FileName = fileName;
content.Add(jsonPart);
content.Add(filePart);
return content;
}
我需要 post 一个请求包含:一个定义为字符串的 xml 和一个 json。我在正确设置内容和处理 JToken 方面遇到问题。
Fiddler 中的原始请求如下所示:
内容类型:multipart/form-data;边界=------------------------acebdf13572468 用户代理:Fiddler 授权:基本QURNSU46QURNSU4= 内容长度:888 主机:localhost
----------------------------acebdf13572468 Content-Disposition:表单数据;名字="fieldNameHere";文件名="testfile2.txt" 内容类型:text/plain
这是第二个测试文件,用于 eB EC 插件测试环境中的文件管理测试。 --------------------------acebdf13572468 Content-Disposition:表单数据;姓名="fieldNameHere" 内容类型:application/json
{ "instance":{ "className": "File", "schemaName": "EB", "relationshipInstances": [ { "direction": "backward", "className": "DocumentFiles", "schemaName": "EB", "relatedInstance":{ "className": "Document", "schemaName": "EB", "instanceId": "4" } } ], "properties":{ "Name": "testfile2.txt" } } }
----------------------------acebdf13572468--
我有这个方法,但出现 404 错误:
public static string PostXMLStringAndJasonInOneRequest(int instanceId)
{
// Creating json that describes document with property 'name'
JObject documentToPost = CreateClassInstance("File", "eB", null, new KeyValuePair<string, object>("Name", "MyTestDocument.txt"));
JObject relationship = CreateRelationshipClassInstance("DocumentFiles", "eB", null, "backward");
// specifying document parent that is existing project with its id
JObject documentParent = CreateClassInstance("Document", "eB", instanceId.ToString());
JToken json = CreateJson(documentToPost, relationship, documentParent);
Uri uri = new Uri("http://localhost/wsg/v2.0/xxxx");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Basic QURNSU46QURNSU4=");
MultipartFormDataContent form = new MultipartFormDataContent();
// TO-DO: save the xml string contents here
HttpContent content = new ByteArrayContent(GetBytes("<node>this is a text</node>"));
// HttpContent content = new StringContent("<node>this is a text</node>");
content.Headers.Add("Content-Type", "application/xml");
form.Add(content);
// TO-DO Save json - not hard coded the value of json
string jsonText = @"{
""instance"": {
""className"": ""File"",
""schemaName"": ""EB"",
""relationshipInstances"": [
{
""direction"": ""backward"",
""className"": ""DocumentFiles"",
""schemaName"": ""EB"",
""relatedInstance"": {
""className"": ""Document"",
""schemaName"": ""EB"",
""instanceId"": ""120""
}
}
],
""properties"": {
""Name"": ""testfile2.xml""
}
}
}";
HttpContent jsonContent = new ByteArrayContent(GetBytes(jsonText));
jsonContent.Headers.Add("Content-Type", "application/json");
// TO-DO put json not jsonText
form.Add(jsonContent);
HttpResponseMessage response = client.PostAsync(uri, form).Result;
if (response.StatusCode != HttpStatusCode.Created)
{
return "error"; // handle error
}
return response.Content.ReadAsStringAsync().Result;
}
}
// To create multipartcontent
// 404 is an error in my url address
private static HttpContent CreateMultipartContent(JToken json, Stream file, string fileName)
{
MultipartContent content = new MultipartContent("form-data", Guid.NewGuid().ToString());
StringContent jsonPart = new StringContent(json.ToString());
jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
jsonPart.Headers.ContentType = new MediaTypeHeaderValue("application/json");
StreamContent filePart = new StreamContent(file);
filePart.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
filePart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
filePart.Headers.ContentDisposition.FileName = fileName;
content.Add(jsonPart);
content.Add(filePart);
return content;
}
private static HttpContent CreateMultipartContent(JToken json, string markupText, string fileName)
{
MultipartContent content = new MultipartContent("form-data", Guid.NewGuid().ToString());
StringContent jsonPart = new StringContent(json.ToString());
jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
jsonPart.Headers.ContentType = new MediaTypeHeaderValue("application/json");
StringContent filePart = new StringContent(markupText);
filePart.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
filePart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
filePar`enter code here`t.Headers.ContentDisposition.FileName = fileName;
content.Add(jsonPart);
content.Add(filePart);
return content;
}