-1

我想使用 http Get 和 Post 来获取特定 URL 请求的请求和响应,

但我不知道如何在目标 c 中使用它们。

在建立连接时,哪个会先获取或发布。?

如何修改内容并将其发布回服务器..

谁能帮帮我吗?

4

2 回答 2

1

供使用:

+(NSMutableURLRequest*)getURq_getansascreen:(NSString*)ws_name {

    NSLog(@"%@",ws_name);

    NSMutableURLRequest *urlReq = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:ws_name] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

    [urlReq addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [urlReq setHTTPMethod:@"GET"];

    return urlReq;
}

后期使用:

+(NSMutableURLRequest*)postURq_getansascreen:(NSString*)ws_name :(NSString*)service {

        NSString *tempUrl = domainURL;

        NSString *msgLength = [NSString stringWithFormat:@"%d",[ws_name length]];
        NSMutableURLRequest *urlReq = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@Service=%@",tempUrl,service]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
        [urlReq addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [urlReq addValue:msgLength forHTTPHeaderField:@"Content-Length"];
        [urlReq setHTTPMethod:@"POST"];
        [urlReq setHTTPBody: [ws_name dataUsingEncoding:NSUTF8StringEncoding]];

        return urlReq;
    }

//在视图中调用这个确实加载为`

WSPContinuous *wspcontinuous = [[WSPContinuous alloc] initWithRequestForThread:[webService getURq_getansascreen:[webService GetDetails:str_filter]] sel:@selector(WS_GetDetailsLoaded:) andHandler:self];`

//创建类 WSPContinuous 并添加这些 fns..

-(id)initWithRequestForThread:(NSMutableURLRequest*)urlRequest sel:(SEL)seletor andHandler:(NSObject*)handler {

    if (self=[super init]) {

        self.MainHandler = handler;
        self.targetSelector = seletor;
        self.urlReq = urlRequest;

        [self performSelectorOnMainThread:@selector(startParse) withObject:nil waitUntilDone:NO];
    }
    return (id)urlReq;
}

-(void)startParse{

    NSLog(@"URL CALLING    %@",urlReq.URL);

    con = [[NSURLConnection alloc] initWithRequest:urlReq delegate:self];
    if (con) {
        myWebData = [[NSMutableData data] retain];

        NSLog(@"myWebData old....%@",myWebData);
    }
    else {
        [self.MainHandler performSelectorOnMainThread:targetSelector withObject:nil waitUntilDone:NO];
    }
}

//-------------------------------connection-----------------

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [myWebData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [myWebData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    [self.MainHandler performSelectorOnMainThread:targetSelector withObject:nil waitUntilDone:NO];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSString *thexml = [[NSString alloc] initWithBytes:[myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];

    NSLog(@"xmlDictionary %@",thexml);

    [thexml release];

    NSError *parseError = nil;
    NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:myWebData error:&parseError];

    [AlertHandler hideAlert];

    [MainHandler performSelector:targetSelector withObject:xmlDictionary];
}
于 2013-02-07T04:57:44.230 回答
0

如果你想开始,一个更好的主意是阅读 NSMutableURLRequest 和相关主题,如 NSURLConnection。

您可以随处获得示例代码。只是谷歌它。

谷歌搜索 ->目标 c 获取并发布

和 First hit -> Tutorials for using HTTP POST and GET on iPhone in Objective-C

于 2013-02-07T05:08:33.980 回答