简体   繁体   English

Xamarin将图像上传到REST Web服务器(错误请求)

[英]Xamarin upload image to a REST web server (Bad Request)

I know that this question has already been posted more than one time but I'm still hanged with the problem of sending one image from a Xamarin client to a REST web server. 我知道这个问题已经发布了不止一次,但是我仍然对从Xamarin客户端向REST Web服务器发送一个图像的问题感到困惑。 I receive a BadRequest error on the client side but I don't know if it comes from the server or from the client. 我在客户端收到BadRequest错误,但我不知道它是来自服务器还是来自客户端。

Here is the Xamarin code (client side) : 这是Xamarin代码(客户端)

public class WsDest
{
   public string D_ID { get; set; }
   public string D_NOM { get; set; }
   public string D_CAT1 { get; set; }
   public string D_CAT2 { get; set; }
   public string D_ANNEE { get; set; }
   public Byte[] D_PHOTO1 { get; set; }
}

static async Task<string> Do_UpdateVehiculeInfos(WsDest Dest)
{
    string cRet = "";
    string cIP = Application.Current.Properties["IPSERVEUR"].ToString().Trim();


    using (HttpClient client = new HttpClient())
    {
        try
        {
            var oJson = JsonConvert.SerializeObject(Dest);
            var cJson = new StringContent(oJson, Encoding.UTF8, "application/json");

            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("Application/json"));
            client.Timeout = TimeSpan.FromMilliseconds(8000);
            client.MaxResponseContentBufferSize = 3000000;
            client.BaseAddress = new Uri(cIP + "/MyWebService.svc/");
            using (HttpResponseMessage r = await client.PostAsync("UpdateVehicule", cJson))
            {
                if (r.IsSuccessStatusCode)
                {
                    await Application.Current.MainPage.DisplayAlert("", "Mise à jour effectuée !", "+OK+");
                    await Application.Current.MainPage.Navigation.PopAsync(); //Remove the page currently on top (= retourne à la page d'avant)
                }
                else
                {
                    await Application.Current.MainPage.DisplayAlert("", r.ReasonPhrase.ToString(), "-OK-");
                }

            }
        }
        catch (Exception e)
        {
            await Application.Current.MainPage.DisplayAlert("", e.Message, "/OK/");
        }
    }
    return cRet;
}

On the server side : 服务器端

[DataContract]
public class WsDest
{
    [DataMember]
    public string D_ID { get; set; }
    [DataMember]
    public string D_NOM { get; set; }
    [DataMember]
    public string D_CAT1 { get; set; }
    [DataMember]
    public string D_CAT2 { get; set; }
    [DataMember]
    public string D_ANNEE { get; set; }
    [DataMember]
    public Byte[] D_PHOTO1 { get; set; }
}

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "UpdateVehicule")]
bool UpdateVehicule(WsDest DestUpdate) ;



public bool UpdateVehicule(WsDest DestRecep)
{
    // my code to process data and image
}

The BadRequest message desapears when I set Dest.D_PHOTO1 to null on client side. 当我在客户端将Dest.D_PHOTO1设置为null时,BadRequest消息消失。

Does anybody have an idea on this subject? 有人对此有想法吗?

Can you show us how the JSON looks like with the byte having a value?<< 您能告诉我们带有字节值的JSON的样子吗?

Before the Json.SerializeObject(Dest), DEST.D_PHOTOS1 is {byte[6855981]}. 在Json.SerializeObject(Dest)之前,DEST.D_PHOTOS1为{byte [6855981]}。 [137,80,70,....] After the serialization, it's something more 'exotic' : [137,80,70,....]序列化之后,它更像“异国情调”:

Before and after serialization 序列化前后

Eric 埃里克

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

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