【发布时间】:2021-08-21 04:25:54
【问题描述】:
Json 字符串的结构如下: {"CODIGO_AGENCIA":"HN001001","CODIGO_USUARIO":"某个用户","CODIGO_CATEGORIA":1}
这是 WS 询问的参数:
公共异步任务 SubirImagenCategoria(string JsonString, HttpPostedFileBase Archivo)
//这是我目前得到的,web服务返回json字符串为空的错误,我完全不知道如何继续。
public static async Task<CustomJsonResult> SubirImagenCategoría(int CodigoCategoria, HttpPostedFileBase Archivo)
{
usuario = UtilClass.GetUsuarioSesion();
var modelo = new SUBIR_IMAGEN_CAT();
modelo.CODIGO_AGENCIA = usuario.CodigoAgencia;
modelo.CODIGO_USUARIO = usuario.Nombre;
modelo.CODIGO_CATEGORIA = 1;
CustomJsonResult result = new CustomJsonResult();
try
{
var JsonString = JsonConvert.SerializeObject(modelo);
var formContent = new MultipartFormDataContent("form-data");
StringContent jsonPart = new StringContent(JsonString.ToString());
jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
jsonPart.Headers.ContentType = new MediaTypeHeaderValue("application/json");
formContent.Add(jsonPart);
/* byte[] Bytes = new byte[Archivo.InputStream.Length + 1];
Archivo.InputStream.Read(Bytes, 0, Bytes.Length);
var fileContent = new ByteArrayContent(Bytes);
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { FileName = Archivo.FileName };
formContent.Add(fileContent);*/
StreamContent filePart = new StreamContent(Archivo.InputStream);
filePart.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
filePart.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
filePart.Headers.ContentDisposition.FileName = Archivo.FileName;
formContent.Add(filePart);
var test = formContent;
/*HttpContent jsonParam = new StringContent(JsonString);
HttpContent fileStream = new StreamContent(Archivo.InputStream);
formData.Add(jsonParam, "JsonString", "JsonString");
formData.Add(fileStream, "Archivo", "Archivo");*/
/*var values = new Dictionary<string, string>
{
{ "JsonString", ("{\"CODIGO_AGENCIA\":"+usuario.CodigoAgencia+",\"CODIGO_USUARIO\":\""+usuario.Nombre+"\" ,\"CODIGO_CATEGORIA\":\""+CodigoCategoria+"\"}") },
};
HttpContent myBody = new FormUrlEncodedContent(values);*/
var formData = new MultipartFormDataContent();
String url = DataEntityLayer.Database.Environment.getFinalUrl(Util.UtilWS.subirImagenesCategorias);
var myHttpClient = new HttpClient();
var response = await myHttpClient.PostAsync(url, formContent);
string stringContent = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<CustomJsonResult>(stringContent);
}
catch (Exception ex)
{
result.Error = ex.Message;
}
return result;
}
【问题讨论】:
-
注释掉的代码是什么,
formData,原来的测试代码是有效的吗? -
您能否确认请求已发送? (使用“fiddler”之类的工具进行捕获,或逐步调试?),您是否尝试过使用“postman”或 curl 之类的工具来测试您正在调用的端点?看着这个,我认为这里的问题可能与“表单数据”有关,但我不确定......我希望它是“多部分/表单数据”。您应该尝试在添加调用时添加/定义名称 -
formContent.Add(jsonPart, "jsonPart")也是如此。DefaultRequestHeaders也在这里考虑.. -
@BrettCaswell 注释代码是我一直在尝试的多个测试之一。此外,我确实有具有请求的 API,有一个断点,我看到 JsonString 参数接收图像就像图像参数一样。我使用邮递员来测试端点,它工作正常,没有标题(默认邮递员已经有),只有两个表单数据参数。此 WS 返回保存图像的 URL 和参考编号。
标签: c# .net asp.net-mvc web-services