私は内蔵のHTTPサーバーを含むアプリケーションを作っています。私はソケットを介してPNGイメージを送信する必要がありますが、何とかそれが混乱していると画像が表示されません。これは私のコードです(AsyncSocketsを使用しています):Cocoaソケットで整形式のHTTP応答を送信するにはどうすればいいですか?
if(processingURL == FALSE) {
NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Contents/Resources/public%@",[self getApplicationPath], [request objectAtIndex:1]];
//NSLog(@"%@", filePath);
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
contententType = @"image/png";
responseCode = @"200 OK";
// responseBody = [[NSString alloc] initWithData:[[NSFileManager defaultManager] contentsAtPath:filePath] encoding:NSASCIIStringEncoding];
NSData * content = [[NSData alloc] initWithContentsOfFile:filePath];
responseBody = [[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding];
NSLog(@"%@",responseBody);
}
}
data = [[NSString stringWithFormat:@"HTTP/1.1 %@\nContent-Type: %@\n\n\n %@", responseCode, contententType, responseBody] dataUsingEncoding:NSUTF8StringEncoding];
[sock writeData:data withTimeout:-1 tag:0];
私は間違っていますか?
部分パスを文字列連結メソッドまたは 'stringWithFormat:'と一緒にステイプルするのではなく、バンドルにイメージのパスまたはURLを問い合わせる必要があります。 –
また、出力を生成する際に、実際にファイルを見つけて読み取ったかどうかにかかわらず、 "contententType"と "responseBody"を持っていると仮定して、結果を割り当てます。 –
こんにちはPeter Hosey。私は何かが失敗した場合に備えて、そのコードの前にデフォルトで自分のボディとヘッダーを設定します。 – pmerino