WebInvoke POST 请求仅在我手动编写 url 时有效
WebInvoke POST request only works if I write the url manually
public async Task<string> insert(string x, string y, string z)
{
using (var client = new HttpClient())
{
var payload = new rootnode { username = x, userpassword = y, usermobile = z };
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(payload));
var entry = new StringContent(stringPayload, Encoding.UTF8, "application/json");
System.Diagnostics.Debug.WriteLine(entry);
var result = await client.PostAsync("http://localhost:20968/Service1.svc/insert/", entry);
return await result.Content.ReadAsStringAsync();
}
}
只有当我使用 entry=""
手动编写 url 时,我的功能才能很好地工作
client.PostAsync("http://localhost:20968/Service1.svc/insert/x,y,z", "entry");
这也是我的 webget 方法
[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json,UriTemplate = "insert/{username}/{userpassword}/{usermobile}")]
鉴于 OP 中规定的 uri 模板,您正在构建错误的请求。已经表明它在手动构建 uri 时有效。发送请求时重复相同的构造。
没有提供有关目标网络方法的足够详细信息,因此以下示例假设使用类似...
[ServiceContract]
public interface IService {
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "insert/{username}/{userpassword}/{usermobile}")]
InsertResponse Insert(string username, string userpassword, string usermobile);
}
通过手动构建请求调用服务可能如下所示
public async Task<string> insert(string x, string y, string z) {
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("http://localhost:20968/Service1.svc/");
//UriTemplate = "insert/{username}/{userpassword}/{usermobile}"
var url = string.Format("insert/{0}/{1}/{2}", x, y, z);
System.Diagnostics.Debug.WriteLine(url);
var request = new HttpRequestMessage(HttpMethod.Post, url);
System.Diagnostics.Debug.WriteLine(request);
var result = await client.SendAsync(request);
return await result.Content.ReadAsStringAsync();
}
}
public async Task<string> insert(string x, string y, string z)
{
using (var client = new HttpClient())
{
var payload = new rootnode { username = x, userpassword = y, usermobile = z };
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(payload));
var entry = new StringContent(stringPayload, Encoding.UTF8, "application/json");
System.Diagnostics.Debug.WriteLine(entry);
var result = await client.PostAsync("http://localhost:20968/Service1.svc/insert/", entry);
return await result.Content.ReadAsStringAsync();
}
}
只有当我使用 entry=""
手动编写 url 时,我的功能才能很好地工作client.PostAsync("http://localhost:20968/Service1.svc/insert/x,y,z", "entry");
这也是我的 webget 方法
[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json,UriTemplate = "insert/{username}/{userpassword}/{usermobile}")]
鉴于 OP 中规定的 uri 模板,您正在构建错误的请求。已经表明它在手动构建 uri 时有效。发送请求时重复相同的构造。
没有提供有关目标网络方法的足够详细信息,因此以下示例假设使用类似...
[ServiceContract]
public interface IService {
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "insert/{username}/{userpassword}/{usermobile}")]
InsertResponse Insert(string username, string userpassword, string usermobile);
}
通过手动构建请求调用服务可能如下所示
public async Task<string> insert(string x, string y, string z) {
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("http://localhost:20968/Service1.svc/");
//UriTemplate = "insert/{username}/{userpassword}/{usermobile}"
var url = string.Format("insert/{0}/{1}/{2}", x, y, z);
System.Diagnostics.Debug.WriteLine(url);
var request = new HttpRequestMessage(HttpMethod.Post, url);
System.Diagnostics.Debug.WriteLine(request);
var result = await client.SendAsync(request);
return await result.Content.ReadAsStringAsync();
}
}