使用AFNetworking向服务器发送POST请求会返回内部服务器错误(500)

时间:2014-04-01 20:32:39

标签: json asp.net-web-api http-post afnetworking-2 internal-server-error

我使用ASP.NET Web Api创建了一个Web服务,并尝试使用AFNetworking 2向这个安静的Web api发送帖子请求。

当我向IIS中的本地服务器发送post请求并使用visual studio 2012进行调试时,每件事情都运行良好,我得到204响应。

但是当我将web api应用程序上传到服务器并发送POST请求时,我收到内部服务器错误(500)

这是我发送POST请求的方式:

static NSString * const BaseURLString = @"http://webapi.r2gblog.ir/";
NSURL *baseURL = [NSURL URLWithString:BaseURLString];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];

manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

//Indicationg this device is online and sending its dEVICE token to the server
Device *device = [Device new];

device.IsOnline = @"True";
//updating current active users of this app in the server
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:
                            device.DeviceToken,@"DeviceToken",
                            device.IsOnline,@"IsOnline",
                            device.LastActivityDate,@"LastActivityDate",
                            device.DeviceModel,@"DeviceModel",
                            nil];

[manager POST:@"api/iosAppStats" parameters:dictionary success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"%@",responseObject);

} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"%@",error);
}];

错误消息:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)" UserInfo=0x175884d0 {NSErrorFailingURLKey=http://webapi.r2gblog.ir/api/iosAppStats, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x17592e90> { URL: http://webapi.r2gblog.ir/api/iosAppStats } { status code: 500, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 0;
Date = "Tue, 01 Apr 2014 20:10:09 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
"X-Powered-By-Plesk" = PleskWin;
} }, NSLocalizedDescription=Request failed: internal server error (500)}

我的WebApi中有一个名为“iosAppStatsController”的控制器,它有一个方法:

// POST api/stats
        public void Post(Device device)
        {
           .
           .
           .
        }

和我的webApiConfig.cs,它位于App_start中 我添加了这个,导致所有响应都是json格式:

var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.Converters = new List<JsonConverter>() { new GuidRefJsonConverter() };
        json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
        config.Formatters.Remove(config.Formatters.XmlFormatter);

使用fiddler 2捕获的POST请求原始文本:

POST http://webapi.r2gblog.ir/api/iosAppStats HTTP/1.1
Host: webapi.r2gblog.ir
Connection: keep-alive
Accept: */*
Accept-Encoding: gzip, deflate
Content-Length: 194
Content-Type: application/json; charset=utf-8
Accept-Language: en;q=1
Connection: keep-alive
User-Agent: Apper/1.0 (iPhone; iOS 7.0.4; Scale/2.00)

{"DeviceToken":"1234567890","LastActivityDate":"2014-04-02T00:40:12.605+0430","IsOnline":"True","OSVersion":"7.0.4","DeviceModel":"iPhone4"}

响应:

HTTP/1.1 500 Version string portion was too short or too long.
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Tue, 01 Apr 2014 20:10:09 GMT
Content-Length: 0

当我将应用程序部署到服务器时,有没有人知道为什么我会收到此内部服务器错误500?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。也许它可以帮助其他人 当您收到内部服务器错误500时,表示您的Web服务崩溃,因为您的控制器发生了内部错误或异常 如果您的Web服务在本地但不在服务器中工作,请再次重新发布项目。您可以通过右键单击解决方案并单击发布在visual studio中执行此操作 然后将所有创建的dll文件上传到您的服务器。还要检查是否将本地web.config替换为服务器上的web.config。小心改变你的连接字符串。

如果您正在使用依赖注入,请确保重新获得依赖关系。如果你忘记解决它,你就不能丰富呼叫动作!

您可以使用Log4net查找代码中发生异常的位置并通过电子邮件发送给您!您也可以将项目bin文件夹中创建的yourWebService.pdb文件上传到服务器。当您的应用程序崩溃时,IIS可以使用上传的pdb文件并确切地告诉代码崩溃发生在哪一行。

最后的话:如果你得到错误500,你必须确保你的代码崩溃。