在IOS上发送HTTP POST请求

在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{


有只小跳蛙
浏览 2669回答 3
3回答

胡子哥哥

我不太确定为什么,但只要我注释掉以下方法,它就会奏效:connectionDidFinishDownloading:destinationURL:此外,我认为您不需要来自NSUrlConnectionDownloadDelegate协议的方法,只需要来自NSURLConnectionDataDelegate的方法,除非您需要一些下载信息。
打开App,查看更多内容
随时随地看视频慕课网APP