为了账号安全,请及时绑定邮箱和手机立即绑定

在IOS上发送HTTP POST请求

在IOS上发送HTTP POST请求

有只小跳蛙 2019-07-06 15:14:50
在IOS上发送HTTP POST请求我正在尝试用我正在开发的iOS应用程序发送HTTPPost,但是推送永远不会到达服务器,尽管我确实获得了响应代码200(来自urlConnection)。我从来没有得到服务器的响应,服务器也没有检测到我的帖子(服务器确实检测到来自Android的帖子)我确实使用ARC,但已经将PD和urlConnection设置为强连接。这是我发送请求的代码 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]                                       initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",dk.baseURL,@"daantest"]]];     [request setHTTPMethod:@"POST"];     [request setValue:@"text/xml"    forHTTPHeaderField:@"Content-type"];     NSString *sendString = @"<data><item>Item 1</item><item>Item 2</item></data>";     [request setValue:[NSString stringWithFormat:@"%d", [sendString length]] forHTTPHeaderField:@"Content-length"];     [request setHTTPBody:[sendString dataUsingEncoding:NSUTF8StringEncoding]];     PushDelegate *pushd = [[PushDelegate alloc] init];     pd = pushd;     urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:pd];     [urlConnection start];这是我的委托代码#import "PushDelegate.h"@implementation PushDelegate@synthesize data;-(id) init{     if(self = [super init])     {         data = [[NSMutableData alloc]init];         [data setLength:0];     }     return self;}- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:     (long long)totalBytesWritten{     NSLog(@"didwriteData push");}- (void)connectionDidResumeDownloading:(NSURLConnection *)connection totalBytesWritten:(long long)     totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes{     NSLog(@"connectionDidResumeDownloading push");}- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destination     URL:(NSURL *)destinationURL{     NSLog(@"didfinish push @push %@",data);}- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWri     tten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{
查看完整描述

3 回答

?
胡子哥哥

TA贡献1825条经验 获得超6个赞

我不太确定为什么,但只要我注释掉以下方法,它就会奏效:

connectionDidFinishDownloading:destinationURL:

此外,我认为您不需要来自NSUrlConnectionDownloadDelegate协议的方法,只需要来自NSURLConnectionDataDelegate的方法,除非您需要一些下载信息。


查看完整回答
反对 回复 2019-07-06
  • 3 回答
  • 0 关注
  • 2668 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信