I am working on porting an application from WPF to a Windows 8 App.
I would like to know if there is a similar functionality like System.Net.WebClient.UploadString in System.Net.Http.HttpClient namespace (since System.Net.WebClient isn’t available in WinRT) If yes an example is greatly appreciated! If not is there any alternative?
On the side note I was able to convert WebClient.DownloadString to its equivalent using System.Net.Http.HttpClient namespace with the help of Morten Nielsen post here http://www.sharpgis.net/post/2011/10/05/WinRT-vs-Silverlight-Part-7-Making-WebRequests.aspx
Yes, you would use the
PostAsyncmethod.The method takes either a
Urior a string (just like theUploadStringmethod on the WebClient class) as well as anHttpContentinstance.The
HttpContentinstance is meant to be agnostic to different types of content, allowing you to specify not only the mechanism by which you provide the content (ByteArrayContentfor an array of bytes, StreamContent for aStream, etc.) but the structure as well (MultipartFormDataContent).That said, there is also a
StringContentclass which will send the string, like so:// The content.
string post = “your content to post”;
If you need to specify an
Encoding, there’s a constructor that takes anEncoding, which you would use like this:From there, it’s a matter of processing the
HttpResponseMessage(if that’s important to you, if it’s not a one-way operation) when the response is sent.