In my oData webservice I have a business object called Ware
. This Ware
has a property of type Image
called Picture
. When I want to get the picture in an UWP application using HttpClient
I just call http://localhost:8797/Data/Ware(2)/Picture
. The code is as follows:
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri(DataServiceAddress + "Data/");
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Clear();
using (HttpResponseMessage message = await client.GetAsync(url))
{
message.EnsureSuccessStatusCode();
var content = (StreamContent)message.Content;
return await content.ReadAsByteArrayAsync();
}
}
This is working as expected and return me the picture as byte of array. Now I want to upload new Picture
by doing a 'PUT'. I've tried the code bellow:
public static async Task PutImage(string url, byte[] image)
{
var handler = new HttpClientHandler { UseDefaultCredentials = true };
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri(DataServiceAddress + "Data/");
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Clear();
using (var ms = new MemoryStream(image))
{
using (var message = await client.PutAsync(url, httpContent))
{
message.EnsureSuccessStatusCode();
}
}
}
}
Then when I'm calling PutImage("Ware(2)/Picture", myFile.toByteArray())
, I'm getting Bad Request
response. Interesting thing is that when I try PUT
with Postman it's working fine. As you can see in the following picture I got 204 result and my picture has changed successfully.
Screenshot: Postman is successful sending the file with a 'PUT' verb to the odata server
Can anyone please help me with that?
This is the generated code by Postman:
PUT /Data/Ware(2)/Picture HTTP/1.1
Host: localhost:8797
Authorization: Basic QWRtaW46
Cache-Control: no-cache
Postman-Token: 37827f09-8841-7ccb-c462-d488ef4c4e9d
undefined
I did a lot of googling. But there is no hope. :(
Finally It's done :)
public static async Task PutImage(string url, byte[] image)
{
var httpContent = new ByteArrayContent(image);
var handler = new HttpClientHandler { UseDefaultCredentials = true };
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri(DataServiceAddress + "Data/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
using (var request = new HttpRequestMessage(HttpMethod.Put, url) { Content = httpContent })
{
httpContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
using (var message = await client.SendAsync(request))
{
message.EnsureSuccessStatusCode();
}
}
}
}
It's working fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.