Http Post for Windows Phone 8

前端 未结 2 1089
[愿得一人]
[愿得一人] 2020-11-28 13:38

I am new to C# so I was wondering if someone can help me out on this. I am trying to send HttpPost from Windows Phone 8 to the server. I found two examples that I would li

2条回答
  •  眼角桃花
    2020-11-28 14:36

    I am also currently working on a Windows Phone 8 project and here is how I am posting to a server. Windows Phone 8 sort of has limited access to the full .NET capabilities and most guide I read say you need to be using the async versions of all the functions.

    // server to POST to
    string url = "myserver.com/path/to/my/post";
    
    // HTTP web request
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.ContentType = "text/plain; charset=utf-8";
    httpWebRequest.Method = "POST";
    
    // Write the request Asynchronously 
    using (var stream = await Task.Factory.FromAsync(httpWebRequest.BeginGetRequestStream,          
                                                             httpWebRequest.EndGetRequestStream, null))
    {
       //create some json string
       string json = "{ \"my\" : \"json\" }";
    
       // convert json to byte array
       byte[] jsonAsBytes = Encoding.UTF8.GetBytes(json);
    
       // Write the bytes to the stream
       await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
    }
    

提交回复
热议问题