我想使用HTTP GET和POST用於獲取請求和某些URL請求的響應,如何編寫http get和post在目標c中?
但我不知道如何在目標C使用它們..
,哪一個會先獲取或建立連接後發佈。?
如何修改內容,並張貼到服務器..
任何一個可以幫我嗎?
我想使用HTTP GET和POST用於獲取請求和某些URL請求的響應,如何編寫http get和post在目標c中?
但我不知道如何在目標C使用它們..
,哪一個會先獲取或建立連接後發佈。?
如何修改內容,並張貼到服務器..
任何一個可以幫我嗎?
爲GET使用:
+(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];
}
我們可以在viewDidLoad方法中寫上面的代碼嗎?哪裏會有響應存儲(HTML格式的響應)?我想C的HTML形式的響應.. –
以上代碼 –
你可以發送給我的整個程序訪問一個Gmail(獲取)頁面和過帳用戶名和密碼,而不使用Gmail的用戶界面..我想打印我在NSLog()中獲得的HTML響應(響應頁面中的內容)。 –
如果你想開始,一個更好的想法是做一些閱讀NSMutableURLRequest和相關的主題,如NSURLConnection。
您可以在任何地方獲得示例代碼。只是谷歌它。
谷歌搜索 - >objective c get and post
,並先打 - >Tutorials for using HTTP POST and GET on the iPhone in Objective-C
發表一些代碼? – hd1
我想開始,所以我想要一些想法.. –
看看ASIHttpRequest或AFNetWorking。他們是很好的libs使用。 – yibuyiqu