如何在asp.net Web方法中修复“改造和Web服务中的500个内部服务器错误”[关闭]

Posted

技术标签:

【中文标题】如何在asp.net Web方法中修复“改造和Web服务中的500个内部服务器错误”[关闭]【英文标题】:how to fix "500 internal server error in retrofit and web services" in asp.net web method [closed] 【发布时间】:2019-12-17 18:10:45 【问题描述】:

我正在尝试使用改造的 android 平台上的 asp.net web-services 插入记录。我已经尝试了很多解决方案,但都没有成功。

在我的asp.net web服务代码中基本上是:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void insertEducationAPI(int user_id, string degree_name, string institute_name, string board_university_name, int year_of_passing, float percentage_cgpa, string specialization)


    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    
        //These headers are handling the "pre-flight" OPTIONS call sent by the browser
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    

    Education edu = new Education();
    string result = educationModel.addEducation(user_id, degree_name, institute_name, board_university_name, year_of_passing, percentage_cgpa, specialization);
    javascriptSerializer js = new JavaScriptSerializer();

    Context.Response.Clear();
    Context.Response.ContentType = "application/json";

    if (result.Equals("complete"))
    
        edu.err = false;
        edu.err_message = "Inserted success";
    
    else
    
        edu.err = true;
        edu.err_message = "Fail";
    
    Context.Response.Write(js.Serialize(edu));

在安卓端

API接口:

@Headers(
        "Accept: application/json",
        "Content-Type: application/json"
)
@FormUrlEncoded
@POST("/EducationApi.asmx/insertEducationAPI")
public void insertEducationDetail(@Field("user_id") int user_id, @Field("degree_name") String degree_name, @Field("institute_name") String institute_name, @Field("board_university_name") String board_university_name, @Field("year_of_passing") int year_of_passing, @Field("percentage_cgpa") float percentage_cgpa, @Field("specialization") String specialization, Callback<Education> callback);


方法调用;

retro.getService().insertEducationDetail(1, "MCA", "SRIMCA", "UTU", 2015, 85, ".NET", new Callback<Education>() 
        @Override
        public void success(Education education, Response response) 
            Toast.makeText(Dashboard.this, "success", Toast.LENGTH_SHORT).show();
        

        @Override
        public void failure(RetrofitError error) 
            Toast.makeText(Dashboard.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        
    );

错误:

I/System.out: [OkHttp] sendRequest<<
D/GraphicBuffer: register, handle(0x87d71180) (w:720 h:1440 s:736 f:0x1 u:b00)
D/Retrofit: <--- HTTP 500 http://192.168.43.25:81/EducationApi.asmx/insertEducationAPI (77ms)
    : HTTP/1.1 500 Internal Server Error
    Cache-Control: private
D/Retrofit: Content-Length: 91
    Content-Type: application/json; charset=utf-8
    Date: Sat, 10 Aug 2019 20:36:09 GMT
D/Retrofit: jsonerror: true
D/Retrofit: Server: Microsoft-IIS/10.0
    X-Android-Received-Millis: 1565469369999
    X-Android-Response-Source: NETWORK 500
    X-Android-Selected-Protocol: http/1.1
    X-Android-Sent-Millis: 1565469369982
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
D/Retrofit: "Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""
    <--- END HTTP (91-byte body)

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

在 Android 端,您的 API 接口看起来不正确:

@POST("/EducationApi.asmx/addEducationAPI")
public void addEducation(@Body List<Education> education, Callback<Education> callback);

它不是服务器端的List&lt;Education&gt;,只是一个Education 对象,所以定义大概应该是:

@POST("/EducationApi.asmx/addEducationAPI")
public void addEducation(@Body Education education, Callback<Education> callback);

== 编辑 ==

另外,您只用[WebMethod] 修饰了您的API 方法,因此它只接受SOAP+XML 输入。要使用 JSON 调用,您需要添加 [ScriptMethod] 装饰,例如:

[WebMethod]
[ScriptMethod]
public void addEducationAPI(Education education)

    //...

由于[ScriptMethod] 属性的安全要求(请参阅JSON Hijacking and How ASP.NET AJAX 1.0 Avoids these Attacks),您需要确保您的改造请求包含Content-Type: application/json 标头。例如:

@Headers("Content-Type: application/json")
@POST("/EducationApi.asmx/addEducationAPI")
public void addEducation(@Body Education education, Callback<Education> callback);

如果 Retrofit 还没有为您这样做,您还应该添加一个 Accept: application/json 标头。例如:

@Headers(
    "Accept: application/json",
    "Content-Type: application/json"
)
@POST("/EducationApi.asmx/addEducationAPI")
public void addEducation(@Body Education education, Callback<Education> callback);

在您的请求中包含这两个标头将允许您删除 JavaScriptSerializer 代码,并只返回一个 Education 的实例。使用[ScriptMethod] 时,ASP.NET Web 服务框架将自动为 SOAP+XML 请求返​​回 SOAP+XML,为 JSON 请求返回 JSON。

所以一个完整的工作示例,其中还包括 EducationApi 类上的 [ScriptService] 属性,如下所示:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class EducationApi : WebService

    EducationModel educationModel = new EducationModel();

    [WebMethod]
    [ScriptMethod]
    public Education addEducationAPI(Education education)
    
        Education edu = new Education();
        string result = educationModel.addEducation(
            education.user_id,
            education.degree_name,
            education.institute_name,
            education.board_university_name,
            education.year_of_passing,
            education.percentage_cgpa,
            education.specialization);
        if (result.Equals("complete"))
        
            edu.err = false;
            edu.err_message = "Inserted success";
        
        else
        
            edu.err = true;
            edu.err_message = "Fail";
        
        return edu;
    

并使用curl 实用程序向其发布数据:

curl --data '"education":"__type":"Education","user_id":1,"degree_name":"MCA","institute_name":"SRIMCA","board_university_name":"UTU","year_of_passing":2015,"specialization":"None","percentage_cgpa":85' --header "Content-Type: application/json" --header "Accept: application/json" --trace-ascii - http://127.0.0.1:8080/EducationApi.asmx/addEducationAPI

返回 JSON 结果:

"d":"user_id":0,"degree_name":null,"institute_name":null,"board_university_name":null,"year_of_passing":0,"specialization":null,"percentage_cgpa":0,"err":false,"err_message":"Inserted success"

【讨论】:

我已经尝试过了,但是不行。同样的错误发生“500 内部服务器”。请建议我另一种解决方案。 我也试过你建议的代码然后它也生成错误“500 internal servers”。 @AkshayJain:500 错误表示出现问题,问题详细信息将在服务器日志中。查看您的服务器日志以了解问题所在。 @halfer:出现此错误:"Message":"处理请求时出错。","StackTrace":"","ExceptionType":""----- ----但我无法理解这个问题。 @AkshayJain:这不是一个糟糕的开始。但是,它似乎缺少一些关键细节,这让我想知道您是否可以提高服务器中的日志级别。它可能设置为“信息”或类似的东西,并且需要设置为“调试”。你能研究一下,看看你的系统是否有可能吗?

以上是关于如何在asp.net Web方法中修复“改造和Web服务中的500个内部服务器错误”[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ASP.NET MVC 中使用路由修复 404?

如何在目标c中的asp.net web api方法中使用POST方法?

ASP.NET Web 表单 - 如何异步调用 WCF 异步方法?

如何在asp.net核心中从客户端调用web api方法?

如何使用 Retrofit 在 android 中调用图像上传 Web 服务 (ASP.NET) .asmx 文件

如何在 ASP .Net (Aspx) 中创建要从 Javascript 访问的 Web 服务方法?