发送 JSON 到服务器 windows phone 8
sending JSON to server windows phone 8
我正在尝试使用 Web 客户端 Class 将 JSON 字符串发送到服务器,但未返回任何结果,我已尝试 Rest Client 插件以确保服务器正在返回一些东西,这里是我的 postRequest 方法`
public static void PostRequest(WebClient webclient,string data,string
url,string header,string method)
{
Uri uri = new Uri(url, UriKind.Absolute);
webclient.Headers[HttpRequestHeader.ContentType] = header;
webclient.UploadStringAsync(uri,method,data);
}
这里是我调用这个方法的地方
string newUserJson="{"User_Name":"yosyos","First_Name":"gfhgas","Last_Name":"jagfshg"}";
wc = new WebClient();
wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wb_UploadStringCompleted);
string url = "http://670b9ada.ngrok.com/laravelProjects/TestVisWall/public/users";
helper.PostRequest( wc, newUserJson, url, "application/json", "POST");
不要使用 WebClient
,Microsoft 不鼓励这样做。
试试 RestSharp 库,在我看来是一个非常好的库。为 Restsharp 添加 recerense,Nuget 方法更快:https://www.nuget.org/packages/RestSharp
RestClient client = new RestClient("baseurl");
var request = new RestRequest("/path/script.php", Method.POST);
request.AddParameter("application/json; charset=utf-8", "json string", ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
client.ExecuteAsync(request, response =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//OK
}
});
}
我正在尝试使用 Web 客户端 Class 将 JSON 字符串发送到服务器,但未返回任何结果,我已尝试 Rest Client 插件以确保服务器正在返回一些东西,这里是我的 postRequest 方法`
public static void PostRequest(WebClient webclient,string data,string
url,string header,string method)
{
Uri uri = new Uri(url, UriKind.Absolute);
webclient.Headers[HttpRequestHeader.ContentType] = header;
webclient.UploadStringAsync(uri,method,data);
}
这里是我调用这个方法的地方
string newUserJson="{"User_Name":"yosyos","First_Name":"gfhgas","Last_Name":"jagfshg"}";
wc = new WebClient();
wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wb_UploadStringCompleted);
string url = "http://670b9ada.ngrok.com/laravelProjects/TestVisWall/public/users";
helper.PostRequest( wc, newUserJson, url, "application/json", "POST");
不要使用 WebClient
,Microsoft 不鼓励这样做。
试试 RestSharp 库,在我看来是一个非常好的库。为 Restsharp 添加 recerense,Nuget 方法更快:https://www.nuget.org/packages/RestSharp
RestClient client = new RestClient("baseurl");
var request = new RestRequest("/path/script.php", Method.POST);
request.AddParameter("application/json; charset=utf-8", "json string", ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
client.ExecuteAsync(request, response =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//OK
}
});
}