Good evening,
I know this subject has already been seen, but I didn't find any solutions to my issue. I'm trying to upload a JPG picture on my server (typical url : http://www.domain.com/upload/{idUser}) and to do that I have been using HttpClient as you can see below
:
public async Task<bool> ExecutePostPictureRequest(string endpoint, Stream stream, string name)
{
bool result;
result = false;
try
{
//Init client
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.ExpectContinue = true;
//Add content
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(CreateFileContent(stream, name, "image/jpeg"));
//Exec request + get response
HttpResponseMessage message = await client.PostAsync(this.url + endpoint, content);
this.json = await message.Content.ReadAsStringAsync();
if (message.IsSuccessStatusCode)
{
result = true;
}
}
catch (Exception exception)
{
Debug.WriteLine("Debug - exception : " + exception.Message);
}
return result;
}
Here is a private method that I use to avoid to have the fileName twice in the headers :
private StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
{
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "uploadfile",
FileName = fileName
};
fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
return fileContent;
}
And this is the server's logs : Bad request is me using the Httpclient and the Good request is me using a simple curl command
-------------------------------------------------- Bad request Begin --------------------------------------------------
|-------------------- 1 request --------------------
| 12:46:03.817722 IP ... > eko.http: Flags [P.], seq 131614028:131614255, ack 492353908, win 32768, length 227
| E...gn@.w...V.
| 9%;)c...P..EL.X.tP...pQ..POST /upload/{userId} HTTP/1.1
| Expect: 100-continue
| Content-Length: 181
| Content-Type: multipart/form-data; boundary="8620d953-fa0c-4e20-b04f-a4741c27081f"
| Host: www.domain.com
| Connection: Keep-Alive
|-------------------------------------------------
|-------------------- 1 response --------------------
| 12:46:03.818124 IP eko.http > ...: Flags [P.], seq 1:26, ack 227, win 237, length 25
| E..A..@.?.lF%;)cV.
| 9.P...X.t..F/P.......HTTP/1.1 100 Continue
|-------------------------------------------------
|-------------------- 2 request --------------------
| 12:46:03.824090 IP ... > eko.http: Flags [P.], seq 227:408, ack 1, win 32768, length 181
| E...go@.w...V.
| 9%;)c...P..F/.X.tP....c..--8620d953-fa0c-4e20-b04f-a4741c27081f
| Content-Dis-data; name=uploadfile; filename=Cover.jpg
| Content-Type: image/jpeg
|
|
| --8620d953-fa0c-4e20-b04f-a4741c27081f--
|-------------------------------------------------
|-------------------- 2 response --------------------
| 12:46:03.830583 IP eko.http > ... Flags [P.], seq 26:260, ack 408, win 245, length 234
| E.....@.?.kt%;)cV.
| 9.P...X....F.P.......HTTP/1.1 500 Internal Server Error
| Server: nginx
| Date: Sat, 08 Oct 2016 10:46:03 GMT
| Content-Type: application/json
| Content-Length: 44
| Connection: keep-alive
| Keep-Alive: timeout=10
|
| {"error":"open : no such file or directory"}
|-------------------------------------------------
-------------------------------------------------- Bad requestEnd --------------------------------------------------
-------------------------------------------------- Good requestBegin --------------------------------------------------
|------------------- 1 request --------------------
| 12:49:20.192774 IP ... > eko.http: Flags [P.], seq 2331120423:2331120671, ack 2271292386, win 229, options [nop,nop,TS val 3764845 ecr 4059129475], length 248
| E..,at@.4..:.u.
| %;)c.~.P....a+......&.....
| .9rm..f.POST /upload/{userId} HTTP/1.1
| Host: www.domain.com
| User-Agent: curl/7.50.3
| Accept: */
| Content-Length: 3457
| Expect: 100-continue
| Content-Type: multipart/form-data; boundary=------------------------9d468a811aeb4aab
|-------------------------------------------------
|------------------- 1 response --------------------
| 12:49:20.193057 IP eko.http > ... Flags [P.], seq 1:26, ack 248, win 235, options [nop,nop,TS val 4059129494 ecr 3764845], length 25
| E..MMm@.?.. %;)c.u.
| .P.~.a+..........].....
| ..f..9rmHTTP/1.1 100 Continue
|-------------------------------------------------
|------------------- 2 request --------------------
| 12:49:20.239665 IP... Flags [P.], seq 248:392, ack 26, win 229, options [nop,nop,TS val 3764859 ecr 4059129494], length 144
| E...av@.4....u.
| %;)c.~.P.....a+............
| .9r{..f.--------------------------9d468a811aeb4aab
| Content-Dis-data; name="uploadfile"; filename="test.jpg"
| Content-Type: image/jpeg
|-------------------------------------------------
|------------------- 3 request --------------------
| 12:49:20.243188 IP ... Flags [.], seq 392:1840, ack 26, win 229, options [nop,nop,TS val 3764860 ecr 4059129494], length 1448
| E...aw@.4....u.
| %;)c.~.P.....a+............
| .9r|..f.......Exif..II*.................Ducky.............-http://ns.adobe.com/xap/1.0/.<?xpacket begin="..." id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27 "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" xmlns:stRef="http://ns.adobe.com/xap/1.0/sType/ResourceRef#" xmp:CreatorTool="Adobe Photoshop CS6 (Macintosh)" xmpMM:InstanceID="xmp.iid:2B6CEE69270011E494FAC9BDB64DE453" xmpMM:DocumentID="xmp.did:2B6CEE6A270011E494FAC9BDB64DE453"> <xmpMM:DerivedFrom stRef:instanceID="xmp.iid:2B6CEE67270011E494FAC9BDB64DE453" stRef:documentID="xmp.did:2B6CEE68270011E494FAC9BDB64DE453"/> </rdf:Description> </rdf:RDF> </x:xmpmeta> <?xpacket end="r"?>....Adobe.d...............................................#%'%#.//33//@@@@@@@@@@@@@@@......................&.....&0#....#0+.'''.+550055@@?@@@@@@@@@@@@......,.,.."..........x................................................................A.!1..Qaq."B...2R....r.#.b.....3Ss$...............................?....S.'x.2c .2c .2c .2c .2c .2c .2c .2c .2c .2c .2c .2c .2c .2c .2c .2c .2c .2....q...;...........................r....I...............................(.q...;.............5.M}.4.Ug....iy.."*...W..)..A........S.........#..4..LS..KVmY..j..:).......k.Z......y).....9K.-].}K5DFxc..x;&6.
|-------------------------------------------------
|------------------- 4 request --------------------
| 12:49:20.243475 IP ... Flags [.], seq 1840:3288, ack 26, win 229, options [nop,nop,TS val 3764860 ecr 4059129494], length 1448
| E...ax@.4....u.
| %;)c.~.P...W.a+............
| .9r|..f..i5;n[.....W..Xl.....k....n.x..Z.............. ..'x........39.........|U..R..h..^...t..WDtv...n.TE.q...."..cOkOn-..).=...P.......a.mj"nZ.....Wkb...Q]....i.......r.uV.Kq..#d.......vLo.........r....I..........b&..i.fg...F........m6c...p7:.%:M=6..;k...R(........4<.G.].M...8W..^.......Mr.s....l...&&bvLl........\.....w.........t...sG7&6..g.l.>.yu=......A...............}.u.cuS......s.p.SW.D}.-h.........\..N...........C8....O...?*.+.Y..;....3...............O..Q.G.-[a.*..a.Z)..e........r....I..............m\.;...vT..9~.....L.'._..ubD.............L..w+....O.."|w|...>.s....Sr..T....@........... ..'x.........r]o.k..'.-.....6.>...WM.s....&.//.....[..t}.............l.UU4S5U8S...........&.l..-.W...y..3..?...Q.............\.....w..............-W.-.4.N.....C.-_...-......l....juS.Gr...n....&...b..U.<.}P.p...@y..Z..7/U...9.4:.msU.j.M.9..v.9.).~.W.\.....YuS--.w-.4\.h.7................. ..'x.......=,i.....T.T.":d.6...\[.L.\..ot.....j0.w|S...d.9}...<Wj...#.........<..[..;....).;%....r..I...,....a;*...i.1..1........O.6|...=.............. ..'x........v..]6..z....M..........._L..~Q..-..c..F..-?|.@(........ 1..N....9.m..m_.f?B.......v.-.v.....Ta0...:.....4N.ut............\..N..........}../z.#.V.s./c_E.\..tF5W1..r.4.z4.(.G.6.L. .P..............I..5V....?..e 8..i....cd..#k.4.....<76W..x.@......... ..x.....L.1Lm..":..nC..\.STl..D.........OE......................yjlS..]..W.vNR....u.n...&bc...A......"<7c
| ..{..........\AxN.....w&.....~.Q....-.....f..Wg
|-------------------------------------------------
|------------------- 5 request --------------------
| ... Flags [P.], seq 3288:3705, ack 26, win 229, options [nop,nop,TS val 3764861 ecr 4059129494], length 417
| E...ay@.4....u.
| %;)c.~.P.....a+............
| .9r}..f..i...................Ng.....1.S....ZH8....?..]..5x{l<@...\.....w.......gdo......+....S...5..E.u.lw...|_c.................E....p.n.F.......-S..............\......qE..$. .....g.(.j..c..d....G.z.-^...DD.Cp.....................t.m..z.....J........G.T..A.....qE..$..p.3gX .:..`...6u.......~j.......p..g....f.................#..T.......}...s....../..0...l...gY...r.l.6a.?..
| --------------------------9d468a811aeb4aab--
|-------------------------------------------------
|------------------- 5 response --------------------
| 12:49:20.576172 IP eko.http > ... Flags [P.], seq 26:247, ack 3705, win 311, options [nop,nop,TS val 4059129590 ecr 3764860], length 221
| E...Mp@.?..Y%;)c.u.
| .P.~.a+...!....7.!.....
| ..f..9r|HTTP/1.1 200 OK
| Server: nginx
| Date: Sat, 08 Oct 2016 10:49:20 GMT
| Content-Type: text/plain; charset=utf-8
| Content-Length: 41
| Connection: keep-alive
| Keep-Alive: timeout=10
|
| {"message":"File uploaded successfully."}
|-------------------------------------------------
-------------------------------------------------- Good End --------------------------------------------------
Looks like the Httpclient is sending the data too quickly... I thank you in advance for your help and I wish you a good day / evening depends where you're from.
Best regards.