我試圖通過網絡上傳相機拍攝的照片到web服務。AFHTTPRequestOperationManager文件上傳返回500服務器錯誤
這是我上傳的部分代碼:
CGSize newSize = CGSizeMake(500.0f, 500.0f);
UIGraphicsBeginImageContext(newSize);
[_imagedata drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imgData = UIImageJPEGRepresentation(newImage, 0.9f);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"user": "test_user"};
[manager POST:@"http://www.mywebsite.com/webservice.aspx" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[formData appendPartWithFormData:imgData name:@"image"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
進出口使用Asp.net作爲我的web服務。
這是我的服務器端代碼:
string USER = Server.UrlDecode(Request["user"]),
SqlParameter prmUser = sc.Parameters.Add("@user", SqlDbType.VarChar);
prmUser.Direction = ParameterDirection.Input;
prmUser.Value = USER;
HttpFileCollection MyFileCollection = Request.Files;
if (MyFileCollection != null && MyFileCollection.Count > 0 && MyFileCollection[0] != null)
{
SqlParameter prmImage = sc.Parameters.Add("@Image", SqlDbType.Image);
prmImage.Direction = ParameterDirection.Input;
byte[] buf = new byte[MyFileCollection[0].ContentLength];
MyFileCollection[0].InputStream.Read(buf, 0, MyFileCollection[0].ContentLength);
prmPhoto.Value = buf;
}
sc.ExecuteNonQuery();
現在每次我運行程序這個錯誤apears:
Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo=0x1555ff60 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x16a94010> { URL: http://www.mywebsite.com/webservice.aspx } { status code: 500, headers {
Connection = close;
"Content-Length" = 3420;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 01 Apr 2015 15:56:21 GMT";
Server = "Microsoft-IIS/8.5";
Via = "1.1 magellan-front (squid/3.5.1)";
"X-AspNet-Version" = "4.0.30319";
"X-Cache" = "MISS from magellan-front";
"X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://www.mywebsite.com/webservice.aspx, NSLocalizedDescription=Request failed: internal server error (500),
詳細的錯誤:
[HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (image="...�5�Ƨ&�����<I
�(�ep��K�,�=mp�...").]
也web服務效果很好用android HttpFileUpload方法。
請在發佈之前設置responseSerializer。你正在把它設置在郵政內。 – Jassi 2015-04-04 12:35:01
這可能是有用的http://stackoverflow.com/questions/81991/a-potentially-dangerous-request-form-value-was-detected-from-the-client – 2015-04-07 13:34:00
下面的鏈接可能是有用的http:// stackoverflow的.com /一個/730807分之29198665,HTTP://stackoverflow.com/a/29198483/730807 – 2015-04-08 16:45:18