如何通过HTTP发布将数据字节发送到.NET服务器 [英] How to send data bytes through HTTP post to .NET server

查看:32
本文介绍了如何通过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:@"http://www.example.org/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服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆