ios - 如何使用 AFNetworking 在 iOS objective-c 中发出此帖子请求
<p><p>我需要从 <a href="https://www.twilio.com/docs/api/rest/sending-messages" rel="noreferrer noopener nofollow">twilio documentation</a> 发出与此等效的 POST 请求在 iOS 中发送短信</p>
<pre><code>curl -XPOST https://api.twilio.com/2010-04-01/Accounts/AC5ef8732a3c49700934481addd5ce1659/Messages.json \
-d "Body=Jenny%20please%3F%21%20I%20love%20you%20<3" \
-d "To=%2B15558675309" \
-d "From=%2B14158141829" \
-u 'AC5ef8732a3c49700934481addd5ce1659:{AuthToken}'
</code></pre>
<p>这就是我所拥有的:</p>
<pre><code>NSString *kTwilioSID = @"AC5ef8732a3c49700934481addd5ce1659";
NSString *kTwilioSecret = @"SECRET";
NSString *kFromNumber = @"+14158141829";
NSString *kToNumber = @"+15558675309";
NSString *kMessage = @"Jenny please?! I love you <3";
NSString *twilioURL = ;
NSDictionary *params = @ {@"account" :kTwilioSID, @"from":kFromNumber, @"to":kToNumber, @"body":kMessage };
AFHTTPRequestOperationManager *manager = ;
[manager POST:twilioURL parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Success: %@", responseObject);
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
</code></pre>
<p>失败并出现错误“请求失败:错误请求 (400)”,并且短信也未发送
我该如何解决这个问题?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我建议你不要使用</p>
<pre><code>[manager POST:twilioURL parameters:params ....
</code></pre>
<p>这个方法对我不起作用。相反,这里有一个有效的代码示例:</p>
<pre><code>NSURL *url = ];
NSMutableURLRequest *request = ;
//1
;
;
NSMutableData *postBody = ;
}",.text, concatCoord] dataUsingEncoding:NSUTF8StringEncoding]];
//post
;
AFHTTPRequestOperation *operation = [ initWithRequest:request];
operation.responseSerializer = ;
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//Do things if succesfull
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//Do things when you've got an error
NSLog(@"error: %@", error);
}];
;
</code></pre>
<p>请注意,我在此代码中使用 JSON 来格式化我的参数 (1)。</p>
<p>祝你好运。</p></p>
<p style="font-size: 20px;">关于ios - 如何使用 AFNetworking 在 iOSobjective-c中发出此帖子请求,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25153554/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25153554/
</a>
</p>
页:
[1]