我试图使用Web客户端类将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");
答案 0 :(得分:0)
不要使用WebClient
,不鼓励Microsoft。
试试RestSharp库,在我看来是一个非常好的库。将Rerensese添加到Restsharp,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
}
});
}