使用Windows.Web.Http.HttpClient上传文件 - WinRT 8.1 PCL

时间:2015-09-15 06:49:24

标签: windows-phone-8.1 httpclient win-universal-app system.net.httpwebrequest

I have a requirement of uploadng the file to Server in Win8.1 PCL project. 
Windows.Web.Http.HttpClient. 
I tried in Advanced Rest Client and it works fine
PFA @ http://i.stack.imgur.com/N8cv5.png

I have the Code like this. 

     Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, uri);

     HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());
     streamContent.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
     streamContent.Headers.Add("Content-Length", stream.Length.ToString());
     streamContent.Headers.Add("Content-Disposition", "form-data; name=\"" + flKey + "\"; filename=\"" + fleNm + "\"");
     request.Content = streamContent;

     var httpClient = new Windows.Web.Http.HttpClient();
     var cts = new CancellationTokenSource();
     Windows.Web.Http.HttpResponseMessage response = await   httpClient.SendRequestAsync(request).AsTask(cts.Token);
     String resp = response.Content.ToString();

这不起作用。请告诉我代码有什么问题,为什么这段代码无法将文件写入服务器。

Thanks in Advance

0 个答案:

没有答案