如何在SOAP web中发送http默认状态码400

本文关键字:默认 http 状态 SOAP web | 更新日期: 2023-09-27 18:02:57

现在对于每个失败结果,我需要发送带有http状态码400的错误消息。如何将错误请求更改为http状态400?

下面是我的代码:
public class Exams : System.Web.Services.WebService
{
[WebMethod]
public string addexam(string cust_name)
{
    if (cust_name=="")
    {
        return "Invalid request";
    }
    BECommon objBECommon = new BECommon();
    objBECommon.cust_name = cust_name;
    string result = objBECommon.DsResult.Tables[0].Rows[0][0].ToString();
    if (result == "1")
    {
        return "Customer exists";
    }
    else 
    {
        return "invalid request";
    }
 // here i need to send the above error message with http status code 400.
}
}

如何在SOAP web中发送http默认状态码400

你可以试试这个

[WebService(Namespace = "http://example.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class Service: System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Result GetData()
    {
        User user = GetUser();
        if (user.LoggedIn)
        {
            return GetData();
        }
        else
        {
            Context.Response.Status = "403 Forbidden"; 
            //the next line is untested - thanks to strider for this line
            Context.Response.StatusCode = 403;
            Context.Response.End(); 
            return null;
        }
    }