美文网首页
http请求头请求体拼接格式

http请求头请求体拼接格式

作者: liboxiang | 来源:发表于2018-12-14 14:46 被阅读46次

青花瓷可以查看具体报文拼接格式

Snip20181217_2.png

POST请求

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
content type为application/x-www-form-urlencoded的例子:

POST / HTTP/1.1
Host: foo.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 13

say=Hi&to=Mom
NSString *url = @"http://218.107.49.181:2050/app/MobileApp.ashx";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
request.HTTPMethod = @"POST";
    
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
NSDictionary *dic = @{@"LoginID":@"shzz",@"Password":@"abc.123",@"IMEI":@"23456"};

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *query = [NSString stringWithFormat:@"Param=%@&Module=Login",jsonString];
    
request.HTTPBody = [query dataUsingEncoding:NSUTF8StringEncoding];

content type为multipart/form-data的例子:

POST /test.html HTTP/1.1 
Host: example.org 
Content-Type: multipart/form-data;boundary="boundary" 

--boundary 
Content-Disposition: form-data; name="field1" 

value1 
--boundary 
Content-Disposition: form-data; name="field2"; filename="example.txt" 
Content-Type: text/plain

value2
--boundary--    //表示结束
request.HTTPMethod = @"POST";
    NSString *boundary = @"Boundary+asdfkljkladjsflkadjsfkladjsfkl";
    [request setValue:[NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
    
    NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [mDic setObject:@"AppUploadFile" forKey:@"Module"];
    [mDic setObject:@"iY4..." forKey:@"Token"];
    
    NSString *dic = @"{\"PSAddrs\":\"广东省广州市黄埔区广州四方邦德实业有限公司\",\"FileTypeID\":\"171\",\"FileID\":\"91afe93a-e2e1-4139-9ff9-03ff534b8958\",\"PSLatitude\":\"\",\"FileDate\":\"2018-12-17 11:17:08\",\"PSLongitude\":\"\",\"PSTime\":\"2018-12-17 11:17:08\",\"BusinessKey\":\"03f74c1e-8e57-4e15-ac68-a438845cc8a0,b1826f99-a468-43a9-865a-fe5a027d21ce\",\"FileName\":\"20181217101021_4177.png\",\"IsUpload\":0}";
    [mDic setObject:dic forKey:@"Param"];
    
    NSMutableData *mData = [NSMutableData dataWithCapacity:0];
    for (NSString *key in mDic.allKeys) {
        NSString *pair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n",boundary,key];
        [mData appendData:[pair dataUsingEncoding:NSUTF8StringEncoding]];
        
        id value = [mDic objectForKey:key];
        if ([value isKindOfClass:[NSString class]]) {
            [mData appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
        }
        [mData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }

    NSMutableString *myString=[NSMutableString stringWithFormat:@"--%@\r\n",boundary];
    [myString appendString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"20181217101021_4177.png\"; filename=\"%@\"\r\n",@"20181217101021_4177.png"]];
    [myString appendString:@"Content-Type: image/jpeg\r\n\r\n"];
    [mData appendData:[myString dataUsingEncoding:NSUTF8StringEncoding]];
    UIImage *image = [UIImage imageNamed:@"test_test"];
    NSData *imageData = UIImagePNGRepresentation(image);
    [mData appendData:imageData];
    [mData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    request.HTTPBody = mData;
    [request setValue:[NSString stringWithFormat:@"%ld",mData.length] forHTTPHeaderField:@"Content-Length"];

注意到在Content-Type里还有个boundary,这个是用来做分隔的字符串。boundary本身没有特殊要求,只要不会和其他内容混淆就好。
换行用\r\n

GET

直接在URL上拼接参数

相关文章

  • http请求头请求体拼接格式

    青花瓷可以查看具体报文拼接格式 POST请求 https://developer.mozilla.org/en-U...

  • HTTP协议和socket连接

    HTTP的格式 HTTP请求、响应报文格式 HTTP请求行、请求头、请求体详解 TCP连接、HTTP连接与Sock...

  • HTTP基础知识整理

    1. HTTP请求报文格式 HTTP请求报文分为三部分:请求行、请求头和请求体 注:部分文章也将HTTP请求报文分...

  • JavaWeb-HTTP协议

    HTTP协议 1. 请求消息体 请求消息:客户端发送给服务器端的数据 数据格式:请求行请求头请求空行请求体 2. ...

  • HTTP请求报文组成

    HTTP请求报文主要分成3个部分:请求行,请求头,请求体,它们从上到下分布 请求行 请求行是一行,主要格式为:[请...

  • 网络教程

    http协议包括 请求协议:请求行、请求头、请求体 响应协议:响应行、响应头、响应体 servlet

  • HTTP请求行、请求头、请求体详解

    HTTP请求行、请求头、请求体详解 HTTP响应头和请求头信息对照表 HTTP请求方法对照表 HTTP状态码对照表...

  • JavaEE-HttpServletRequest总结

    HttpServletRequest: 封装了Http请求内容(请求行, 请求头, 请求体) 1.HTTP请求行和...

  • Nginx笔记 2018-01-11

    request 在nginx中我们指的是http请求,一个http请求,包含请求行、请求头、请求体、响应行、响应头...

  • Servlet与JSP进阶笔记(一)

    请求与相应 HTTP请求的结构 HTTP 请求包含三部分:请求行,请求头,请求体 POST请求方法有请求体,而GE...

网友评论

      本文标题:http请求头请求体拼接格式

      本文链接:https://www.haomeiwen.com/subject/zelghqtx.html