我在我的服務器上有一個ASP.NET MVC。 Iphone正在向ASP.NET MVC控制器發送一個Http請求。下面是iPhone的代碼,服務器沒有從iPhone獲取正確格式的數據?
-(void)addNSData:(NSData*)datapart inData:(NSMutableData*)POSTBody forkey:(NSString*)key
{
[POSTBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[POSTBody appendData:[[NSString stringWithFormat:@"%@", datapart] dataUsingEncoding:NSUTF8StringEncoding]];
[POSTBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
{
NSMutableURLRequest *request = nil;
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://XXX/XXX/publish"]];
NSString *jsonFilePath = [[SharedData shareData].pageDataFolderPath stringByAppendingPathComponent:@"page_json.json"];
[ request setHTTPMethod: @"POST" ];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
[request addValue:@".ASPXAUTH=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" forHTTPHeaderField:@"Cookie"];
// Add HTTP Body
NSMutableData *POSTBody = [NSMutableData data];
[POSTBody appendData:[[NSString stringWithFormat:@"--%@\r\n",POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[self addNSData:[NSData dataWithContentsOfFile:localZipPath] inData:POSTBody forkey:@"zipFile"];
[self addNSData:[NSData dataWithContentsOfFile:jsonFilePath] inData:POSTBody forkey:@"contents"];
NSString *v = page.mo_page.version;
[self addNSData:[v dataUsingEncoding:NSUTF8StringEncoding] inData:POSTBody forkey:@"version"];
[self addNSData:[@"true" dataUsingEncoding:NSUTF8StringEncoding] inData:POSTBody forkey:@"isActive"];
[self addNSData:[page.mo_page.pageid dataUsingEncoding:NSUTF8StringEncoding] inData:POSTBody forkey:@"directory"];
// Add the closing -- to the POST Form
[POSTBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Add the body to the myMedRequest & return
[request setHTTPBody:POSTBody];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(error)
{
NSLog(@"Error if any:%@",error.localizedDescription);
}
else {
NSLog(@"Response : %@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}
[[NSFileManager defaultManager] removeItemAtPath:localZipPath error:nil];
localZipPath = nil;
}
的ASP.NET MVC服務器獲取數據以這種格式,例如
Request.Form["Directory"]= "<32383862 33363339 36393538 34653463 62643862 35376561 66653135 35313764>"
謝謝。你能提供一些示例代碼嗎? – user960567 2013-02-12 17:09:32
你應該看看你的代碼,你在哪裏引用數據。你的代碼正如我所說的將NSData對象視爲一個字符串,因此它返回NSString @「<3333 4444 5555 ....」。 – 2013-02-12 21:48:52
謝謝我已經替換了'[POSTBody appendData:[[NSString stringWithFormat:@「%@」,datapart] dataUsingEncoding:NSUTF8StringEncoding]]'''POSTBody appendData:datapart];'它可以工作。現在它的工作但現在zipFile爲空 – user960567 2013-02-13 06:06:30