How to send a POST request via WebKit - objective-c

To navigate a WebKit WebView to a URL is quite easy:
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[m_webView mainFrame] loadRequest:requestObj];
I can't see where/how to add POST data to any of these objects. How do one perform a Post, using a hosted WebView in Cocoa?
Sample code for the answer:
NSData* postData = postString;
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSString* contentLength = [NSString stringWithFormat:#"%d", [data length]];
[urlRequest setValue:contentLength forHTTPHeaderField:#"Content-Length"];
[urlRequest setHTTPBody:postData];
[[m_webView mainFrame] loadRequest:urlRequest];

Use an NSMutableURLRequest object. It has a setHTTPMethod: method.

Related

NSMutableURLRequest returns old values even cachePolicy is NSURLCacheStorageNotAllowed

Im using codes posted here:
connection release method in connectionDidFinishLoading, causes error
now first execute returns didFail log.
second execute; returns old response data.
albeit my (localhost) server is totally offline.
and cachePolicy is NSURLCacheStorageNotAllowed (check the code on the link I posted above)
NSMutableURLRequest *request=
[NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:3.0f];
the response data seems cached somewhere and still exists.
but if I use NSURLRequestReloadIgnoringLocalAndRemoteCacheData //which is commented as -not implemented-
not returns old cache.
but if so what is the difference between:
NSURLRequestReloadIgnoringLocalAndRemoteCacheData
and
NSURLCacheStorageNotAllowed
what shall I do ?
NSURLCacheStorageNotAllowed refers to NSCachedURLResponse and is an value of enum NSURLCacheStoragePolicy. Since the cache policy of NSMutableURLRequest is also an enum (NSURLRequestCachePolicy) you just pass wrong int to the static method creating NSMutableURLRequest. In this case NSURLCacheStorageNotAllowed is just 2 which equals to NSURLRequestReturnCacheDataElseLoad - and that is why you get old data.
Try This
NSString *Post = [[NSString alloc] initWithFormat:#"Post Parameters"];
NSURL *Url = [NSURL URLWithString:#"Url"];
NSData *PostData = [Post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [PostData length]];
NSMutableURLRequest *Request = [[NSMutableURLRequest alloc] init];
[Request setURL:Url];
[Request setHTTPMethod:#"POST"];
[Request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[Request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[Request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[Request setHTTPBody:PostData];
NSError *error;
NSURLResponse *response;
NSData *Result = [NSURLConnection sendSynchronousRequest:Request returningResponse:&response error:&error];
if (!Result)
{
NSLog(#"Error");
}
else
{
//Parse the result
}

Can't obtain a response from WCF service

i have a trouble when i try to call a web service made in WCF, this service is calling by Mac os x app.
this is the code
soapMessage = #"{name:\"Sergio\"}";
NSURL *url = [NSURL URLWithString:#"http://localhost/GSBonoServicios/GSBonoService.svc/HelloWorld"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest setHTTPMethod: #"POST"];
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSData *requestData = [NSData dataWithBytes:[soapMessage UTF8String] length:[soapMessage length]];
[theRequest setHTTPBody:requestData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
The connection is successful but didn't a response, i try implementing some tutorials and I'm looking in Stackoverflow and i find many answers but nothing works, i hope you can help me.
ps. I do not speak much English, but I hope I explained, Thanks
Finally I found the solution the problem was in the service, and the code that works is
NSString *name=#"Sergio";
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys: name, #"name",
nil];
NSURL *url = [NSURL URLWithString:#"http://www.myserver.com/Servicio/ServicioEjemplo.svc/HelloWorld"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod: #"POST"];
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"applicat ion/json" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:kNilOptions error:&error];
[theRequest setHTTPBody:postdata];
NSLog(#"JSON summary: %#", [[NSString alloc] initWithData:postdata
encoding:NSUTF8StringEncoding]);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
Thanks

sending data using CURL in objective c [duplicate]

This question already has answers here:
Tutorials for using HTTP POST and GET on the iPhone in Objective-C [closed]
(5 answers)
Closed 9 years ago.
how i have to use the following CURL command in objective c,
curl -d lang=fr -d badge=0 http://www.redis.com/subscriber/J8lHY4X1XkU
Thanks in advance
You could try:
NSURL *url = [NSURL URLWithString:#"http://www.redis.com/subscriber/J8lHY4X1XkU"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString* str = #"lang=fr&badge=0";
NSData* body = [str dataUsingEncoding:NSUTF8StringEncoding];
[req setHTTPMethod:#"POST"];
[req setHTTPBody:body];
[req setValue:[NSString stringWithFormat:#"%d", [str length]] forHTTPHeaderField:#"Content-length"];
[[NSURLConnection alloc] initWithRequest:req delegate:self];
More about this you can find on iOS documentation: NSURLConnection and NSMutableURLRequest
Can you try following code?
NSURL *url = [NSURL URLWithString:#"http://www.redis.com/subscriber/J8lHY4X1XkU?lang=fr&badge=0"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120];
[request setURL:url];
[request setHTTPMethod:#"GET"];
NSData *responce = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];

xml post as parameter for Objective C

How does one post xml as parameter with Objective C?
I've sent a username and a password in xml to the server. I've tried using ASIFormDataRequest. I've posted xml and the server've given the error that is "username or password is false". I think the server doesn't parse the posting xml. Is there any way to post xml as parameter?
NSURL *url = [NSURL URLWithString:#"url code"];
ASIFormDataRequest *request1 =[ASIFormDataRequest requestWithURL:url];
[request1 setPostValue:"xml block" forKey:#"data"];
[request1 addRequestHeader:#"Content-Type" value:#"application/xml;"];
[request1 setDelegate:self];
[request1 startSynchronous];
Yes, it is. Look at my example:
NSString *message = [[NSString alloc] initWithFormat:#"<?xml version=\"1.0\" ?>\n<parameters></parameters>"];
url = [NSURL URLWithString:#"https://https_url.com"];
request = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d",[message length]];
[request addValue:#"application/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];
[message release];

Where to put Loading

In my app I have connections with a web service and I would like to create a loading view to show each time the connection starts until it returns a response.
Where is the best place to put this view in my application and what is the best way to implement it?
Thanks,
Fermin
NSURL *url = [NSURL URLWithString:#"http://.....?WSDL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[theConnection start];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];
}