I have an project in which i retrieve data from user and save in local xml file. I have done all this on simulator. Now i want to deploy on iPhone device. But problem is that how send file (XML) from iphone to server by using web services?
You can HTTP POST it:
NSString * xmlString = #"<test><message length="5">Hello</message></test>";
NSURL * serviceUrl = [NSURL URLWithString:#"http://my.company.com/myservice"];
NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setValue:#"text/xml" forHTTPHeaderField:#"Content-type"];
[serviceRequest setHTTPMethod:#"POST"];
[serviceRequest setHTTPBody:[xmlString dataUsingEncoding:NSASCIIStringEncoding]];
NSURLResponse * serviceResponse;
NSError * serviceError;
serviceResponse = [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];
Taken from here
Related
When I connect to web services that return Json strings it returns them like XML:
2014-02-07 00:17:15.673 Test[791:70b] <\?xml version="1.0" encoding="utf-8"?>
<\string xmlns="MBSCafe.Service.ServiceManager">{"query": [{"ID": "01","Name": "food"}]}<\/string>
Instead, I want this:
{"query": [{"ID": "01","Name": "food"}]}
This is my part of code:
NSString *urlString = #"http://mbsserver/MBSCafeService/ServiceManager.asmx/GetItemTypeList";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Accept"];
NSString *myRequestString = #"id="; // Attention HERE!!!!
myRequestString = [myRequestString stringByAppendingString:#"01"];
NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]];
[request setHTTPBody: requestData];
What is wrong with it?
Make sure server return response in json format first, you can check it by fidder or other Firefox, Chrome plugins
Test your webservice using the firefox plug-in called "HTTP resource test", specify your parameters there and see what response you are getting. You can find this plug-in using firefox->tools->HTTP resource test.
I want to follow a twitter user on a button click. I used the share kit follow me method using oauth. But always I am getting this error.
{"error":"Could not authenticate with
OAuth.","request":"/1/friendships/create/priyankav89.json"}
Then I tried to implement it myself
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1/friendships/create.json"];
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:#"871252998" forKey:#"user_id"];
[dict setObject:#"true" forKey:#"follow"];
[dict setObject:#"KXL0jFLJY6RI4lszOz8r5w" forKey:#"consumer"];
NSString *jsonRepresentation=[dict JSONString];
NSLog(#"jsonRepresentation = %#",jsonRepresentation);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3];
NSData *postData=[NSData dataWithBytes:jsonRepresentation.UTF8String length:[jsonRepresentation length]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [postData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error=nil;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"respon = %s",responseData.bytes);
still I am getting the same error.
I searched a lot. Still no use. I don't want to load the url in a UIWebView or in safari. I want to support the app from ios4. So ios5 built-in twitter app cannot be used.
I found the problem. In share kit follow me method they are not calling the authorization method before following. So we want to call authorize before login and save the access token.
Assuming you know how to tweet using the twitter integrated to the iOS, you can simply tweet "follow "
Actually I am working on the Couchdb .Till now i have used the Local server for Connecting to he CouchDb . Now I need to Connect to the remote server of couchdb . For this I need to send the User Name And Password Through the Json .
How can we use authenticated basic Json post ?
use the post method (post the username and password )
code like …. try
NSString *postStr = [NSString stringWithFormat:#"username=%#&password=%#", username, password];
NSData *postData = [postStr dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *requesting = [[NSMutableURLRequest alloc] initWithURL:YourURLObject];
[requesting setHTTPMethod:#"POST"];
[requesting setValue:[NSString stringWithFormat:#"%d", [postData length]] forHTTPHeaderField:#"Content-Length"];
[requesting setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[requesting setHTTPBody:postData];
NSError *error;
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:requesting returningResponse:&response error:&error];
Add JSON framework if you are using iOS5 sdk else download SBJSON and add it to your project and import "SBJSON.h"
Case 1: Using JSON Array
NSArray *credentials = [NSArray arrayWithObjects:#"uName", #"pWord",nil];
NSString *jsonString = [credentials JSONString];
Case 2: Using JSON Dictionary
NSDictionary *credentials = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"uName", #"pWord",nil] forKeys:[NSArray arrayWithObjects:#"UserName",#"password",nil]];
NSString *jsonString = [credentials JSONString];
I have an iphone app that I'm trying to get to talk to a rails back end. I'm using NSMutableURLRequest to pull data back and forth. All the calls work fine on GET requests but when I need to post data, my rails app can't seem to find the session. I've posted the code below for both a get and a POST request.
This is the POST request:
//Set up the URL
NSString *url_string = [NSString stringWithFormat:#"https://testserver.example.com/players.xml"];
NSURL *url = [NSURL URLWithString:url_string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
//To create an object in rails
//We have to use a post request
////This is the format when using xml
NSString *requestString = [[NSString alloc] initWithFormat:#"<player><team_game_id>%#</team_game_id><person_id>%#</person_id></player>", game, person];
NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:requestData];
NSString *postLength = [NSString stringWithFormat:#"%d", [requestData length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
//For some reason rails will not take application/xml
[request setValue:#"application/xml" forHTTPHeaderField:#"content-type"];
The get request is:
NSString *url_string = [NSString stringWithFormat:#"https://testserver.example.com/people/find_by_passport?passport=%i", passport];
passportString = [[NSMutableString alloc] initWithFormat:#"%i", passport];
NSLog(#"The passport string is %#", passportString);
NSLog(url_string, nil);
NSURL *url = [NSURL URLWithString:url_string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
I am at my wits end here trying to find out whats going on so any help would be GREATLY appreciated.
Faced the same issue. Parse the response headers for the Set-Cookie header, extract the cookies by hand, pass them back as Cookie: header on subsequent requests.
If this is just about sessions in their simplest, you don't have to worry about persistence, HTTPS, domains/paths and such.
EDIT: I found class NSHTTPCookieStorage. See if it can be of help...
i want to upload an
UIImage
to a server.
the problem i dont know how to add it to my post to the server.
until now i sent post with thia:
NSString *reqURL = [NSString stringWithFormat:#"url"];
reqURL = [reqURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
there is any different way to do for image?
Use ASIHttpRequest
-(void)uploadImage{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// Upload a file on disk
[request setFile:#"/Users/ben/Desktop/ben.jpg" withFileName:#"myphoto.jpg" andContentType:#"image/jpeg"
forKey:#"photo"];
// Upload an NSData instance
[request setData:UIImageJPEGRepresentation(myUIImage) withFileName:#"myphoto.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
}
This answer points to this wrapper framework that should make it easy to do what you want without dealing with HTTP header strings and so on.
Basically, you'll turn your UIImage into an NSData object using a function like UIImagePNGRepresentation() and then upload it over HTTP.