gpt4 book ai didi

ios - 使用 NSMutableURLRequest 的字符串请求

转载 作者:行者123 更新时间:2023-11-29 00:31:47 25 4
gpt4 key购买 nike

我必须向服务器发送 base64 字符串请求。

代码:

NSString *body = [NSString stringWithFormat:@"[\"Login\",{\"password\":\"%@\",\"username\":\"%@\",\"ip\":\"%@\",\"login_type\":\"IOS\",\"short_name\":null}]",self.mPassword.text,self.mUsername.text,[Settings getIPAddress]];

NSLog(@"login %@",body);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[Settings getLoginUrl]]];
NSData *postData = [body dataUsingEncoding:NSDataBase64Encoding64CharacterLineLength allowLossyConversion:YES];

[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

但是它向服务器发送了一个空字符串。为什么这样?我希望将 base64 字符串作为请求发送到服务器。

更新:

NSString *body = [NSString stringWithFormat:@"[\"Login\",{\"password\":\"%@\",\"username\":\"%@\",\"ip\":\"%@\",\"login_type\":\"IOS\",\"short_name\":null}]",self.mPassword.text,self.mUsername.text,[Settings getIPAddress]];



NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[Settings getLoginUrl]]];
NSLog(@"%@",[Settings getLoginUrl]);
NSData *postData = [body dataUsingEncoding: NSUTF8StringEncoding];
NSString *base64EncodedStr = [postData base64EncodedStringWithOptions:0];
NSData *base64EncodedData = [[NSData alloc] initWithBase64EncodedString:base64EncodedStr options:0];
[request setHTTPBody:base64EncodedData];
[request setHTTPMethod:@"POST"];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[base64EncodedData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *connectionError)
{
if ([data length] > 0 && connectionError == nil)
{
[self receivedLoginData:data];

}
else if ([data length] == 0 && connectionError == nil)
{
[self emptyReply];
}
else if (connectionError != nil && connectionError.code == NSURLErrorTimedOut)
{
[self timedOut];
}
else if (connectionError != nil)
{
[self downloadError:connectionError];
}
}]resume];

最佳答案

NSDataBase64Encoding64CharacterLineLength 是一个 NSDataBase64EncodingOption 而不是 Base64Encoding。

你需要使用例如this solution

关于ios - 使用 NSMutableURLRequest 的字符串请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41583679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com