青花瓷可以查看具体报文拼接格式
Snip20181217_2.pngPOST请求
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上拼接参数
网友评论