Created
January 15, 2019 02:27
-
-
Save if1live/eeda9fdaec52a64033f6de21ddececda to your computer and use it in GitHub Desktop.
C# HttpClient + Unity + GET + json body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Json; | |
using System.Text; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
using System.Net.Http; | |
using System.Net; | |
using System.Threading.Tasks; | |
[DataContract] | |
internal class SimpleReq | |
{ | |
[DataMember] | |
public string foo; | |
} | |
public class Demo : MonoBehaviour | |
{ | |
async void Start() | |
{ | |
await BeginNetwork(); | |
} | |
public static string ToJsonString<T>(T data) | |
{ | |
var stream1 = new MemoryStream(); | |
var ser = new DataContractJsonSerializer(typeof(T)); | |
ser.WriteObject(stream1, data); | |
stream1.Position = 0; | |
StreamReader sr = new StreamReader(stream1); | |
var jsonBody = sr.ReadToEnd(); | |
return jsonBody; | |
} | |
private static readonly HttpClient client = new HttpClient(); | |
/* | |
ProtocolViolationException: Cannot send data when method is: GET | |
*/ | |
async Task<bool> BeginNetwork() | |
{ | |
var host = "http://127.0.0.1:3100"; | |
var path = "/v1/user/mydata"; | |
var body = ToJsonString(new SimpleReq() | |
{ | |
foo = "bar", | |
}); | |
var request = new HttpRequestMessage(HttpMethod.Get, $"{host}{path}") | |
{ | |
Content = new StringContent(body, Encoding.UTF8, "application/json"), | |
}; | |
Debug.Log(request.Headers); | |
var resp = await client.SendAsync(request); | |
Debug.Log(resp.StatusCode); | |
Debug.Log(await resp.Content.ReadAsStringAsync()); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment