如何通过HTTP发布将数据字节发送到.NET服务器

编程入门 行业动态 更新时间:2024-10-14 14:18:07
本文介绍了如何通过HTTP发布将数据字节发送到.NET服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

将表单的enctype设置为"multipart/form-data"会导致Web服务抛出异常.

Setting the form's enctype to "multipart/form-data" causes an exception to be thrown by the webservice.

System.InvalidOperationException:请求格式无效:multipart/form-data;boundary = --.....

System.InvalidOperationException: Request format is invalid: multipart/form-data; boundary=---.....

我需要直接将HTML表单发布到Web服务,并且它不能由.NET服务器托管.

I need to post the HTML form direct to the webservice, and it can't be hosted by the .NET server.

任何想法...

谢谢.

推荐答案

请参考 RFC 1867了解多部分协议的含义.您需要手工制作多部分的装饰",此代码可能会有所帮助:

Please refer to the RFC 1867 to understand what a multipart protocol means. You need to make the multipart "decoration" by hand, this code may helps:

-(NSData *)makeMultipartDataWithBoundary:(NSString *) boundary parameters:(NSDictionary *)parameters formName:(NSString *)formName fileName:(NSString *)fileName fileType:(NSString *)fileType fileData:(NSData*)fileData { NSMutableString * res = [NSMutableString stringWithFormat:@"--%@\r\n", boundary]; for(NSString * key in [parameters allKeys]){ NSString * value = [parameters objectForKey:key]; if(![value isMemberOfClass:[NSString class]]) continue; [res appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key]; [res appendFormat:@"\r\n%@\r\n", value]; [res appendFormat:@"--%@\r\n", boundary]; } [res appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", formName, fileName]; [res appendFormat:@"Content-Type: %@\r\n\r\n", fileType]; NSMutableData * data= [NSMutableData data]; NSLog(@"%@", res); [data appendData:[res dataUsingEncoding:NSUTF8StringEncoding]]; [data appendData:fileData]; [data appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; return data; }

其中:

边界是一个NSString(字节序列),未出现在fileData部分中.例如'D33pN16h7abcd'

boundary is a NSString (bytes sequence) that doesn't be appear in the fileData section. e.g. 'D33pN16h7abcd'

parameters 是包含所有要传递给multipart协议的标头参数的字典.

parameters is a dictionary contaning all the header parameters to be passed to the multipart protocol.

formName 是一个标识符.

fileType 是mime文件类型,例如'image/png'

fileType is the mime file type e.g. 'image/png'

最后 fileData 是文件内容的字节.

Finally fileData are the bytes of the content of file.

接下来的几行显示了调用此方法的示例:

The next lines shows yo a example to call this method:

- (void) sendAllData { NSData *data; data = [self makeMultipartDataWithBoundary:kBoundary parameters:nil formName:@"imageField" fileName:@"imageExample.png" fileType:@"image/png" fileData:self.imageData]; // Make a post NSURLConnection request with the data NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"www.example/file.php"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:data]; [request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", kBoundary] forHTTPHeaderField:@"Content-Type"] NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (connection) { receivedData = [[NSMutableData data] retain]; } }

希望这会有所帮助!:)

Hope this helps! :)

更多推荐

如何通过HTTP发布将数据字节发送到.NET服务器

本文发布于:2023-10-11 14:18:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   字节   服务器   数据   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!