如何在web服务中读取多部分/表单数据?我是用邮递员的身体表格-数据发送数据,但邮递员是错误的。
public class Api : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string Hell(string name)
{
return CommonUtilities.GetJSonSerialized(name);
}
}错误System.InvalidOperationException:请求格式无效:多部分/表单-数据;System.InvalidOperationException在System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
邮递员犯了那个错误。
发布于 2018-11-12 08:33:34
Asmx文件也可以用于rest的创建(这不是推荐的方法)。
这可以通过下面的代码片段来实现。
[ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Randezvous : WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void getUnitPersonels(string user, string pass, decimal unitNo)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType = "application/json";
#region ..:: Kullanıcı şİfre Kontrol ::..
if (!(unit == "xxx" && pass == "yyy"))
{
string msg = "User or pass is wrong.";
Context.Response.Write(serializer.Serialize(msg));
return;
}
#endregion
List<Personels> personels = _units.getUnitPersonels(unitNo);
string jsonString = serializer.Serialize(personels);
Context.Response.Write(jsonString);
}
}您可以使用下面所示的代码在c#中测试此代码:
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var builder = new UriBuilder("http://localhost:18511/Randezvous.asmx/getUnitPersonels");
var query = HttpUtility.ParseQueryString(builder.Query);
query["unitNo"] = "0";
builder.Query = query.ToString();
string url = builder.ToString();
var result = Task.FromResult(client.GetAsync(url).Result).Result.Content;
var resultJson = result.ReadAsStringAsync().Result;
}https://stackoverflow.com/questions/53257461
复制相似问题