0

I tried with below code. Here the problem is parameters are directly passing to the database from url.But i need to send the data from Text fields.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http:UserProfile.svc/InsertUserDetailsNew?Name=RajeevLeader&FirstName=rajeev&DateofBirth=2012-02-03&Sex=M&Country=India&City=Banglore&PhoneNumber=8523898947&IncumbentonPremimum=true"]];


NSLog(@"url is %@",request);

NSString *requestString =[NSString stringWithFormat:@"Name=%@&FirstName=%@&DateofBirth=%@&Sex=%@&Country=%@&City=%@&PhoneNumber%@&IncumbentonPremimum=%@",name.text, firstname.text,dob.text,gender.text,country.text,city.text,phone.text,premiumtextfield.text];

NSLog(@" RequestString: %@",requestString);

NSMutableData *requestData =[NSMutableData dataWithBytes:[requestString UTF8String] length: [requestString length]];

[request setHTTPMethod: @"POST"];

[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody: requestData];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (con)
{
    NSLog(@"data sent ");
} else
{
    NSLog(@"Not sent");
}

[con start];
  • possible duplicate of [Sending HTTP-Post ios](http://stackoverflow.com/questions/15749486/sending-http-post-ios) – iPatel Jun 04 '14 at 05:37
  • have u say the correct url, u were passing the url in full value, it is the wrong one, – Anbu.Karthik Jun 04 '14 at 05:47
  • if u r given the valid url , I given the answer whatever u need – Anbu.Karthik Jun 04 '14 at 05:50
  • @Anbu.Karthik here is anyway to pass the data from textfields using url –  Jun 04 '14 at 05:53
  • copy your url, open the new browser and paste ur url,finally u check and tell what the answer u get in browser – Anbu.Karthik Jun 04 '14 at 05:54
  • http://192.168.2.4:98/UserProfile.svc/InsertUserDetailsNew?Name=tillu&FirstName=killu&DateofBirth=2012-02-03&Sex=M&Country={Country}&City=ban&PhoneNumber=125875&IncumbentonPremimum=true if entered this url in browser what ever i mentioned in the url for name: tillu firstname: killu is directly passing into database. but instead of this i need get the data from textfields –  Jun 04 '14 at 05:59
  • its like a Hotcode @Anbu.Karthik –  Jun 04 '14 at 06:04

2 Answers2

0

you have to pass parameters afterwords & not with the url.

Divyang Shah
  • 1,578
  • 1
  • 11
  • 22
0

just change your URL for the following method

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://192.168.2.4:98/UserProfile.svc/InsertUserDetailsNew"]];



   NSString *requestString = [[NSString alloc]
                  initWithFormat:@"{ \"Name\" : \"%@\" , \"FirstName\" : \"%@\" , \"DateofBirth\" : \"%@\" , \"Sex\" : \"%@\", \"Country\" : \"%@\", \"City\" : \"%@\", \"PhoneNumber\" : \"%@\" , \"FirstName\" : \"%@\"}",
                  name.text, firstname.text,dob.text,gender.text,country.text,city.text,phone.text,premiumtextfield.text];


NSLog(@" RequestString: %@",requestString);

NSMutableData *requestData =[NSMutableData dataWithBytes:[requestString UTF8String] length: [requestString length]];




[request setHTTPMethod: @"POST"];




 [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];




 [request setHTTPBody: requestData];
  NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[con start];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • in which server u have used .php or java in which server u have used – Anbu.Karthik Jun 04 '14 at 06:15
  • webserver[930:a0b] data sent webserver[930:a0b] didReceiveResponse: responseData length:(0) webserver[930:a0b] parsing JSON: (null) –  Jun 04 '14 at 06:18
  • http://192.168.2.4:98/UserProfile.svc/InsertUserDetailsNew.? -- which type of the extension u have used – Anbu.Karthik Jun 04 '14 at 06:19
  • extension am not getting http://192.168.2.4:98/UserProfile.svc/InsertUserDetailsNew i just used it –  Jun 04 '14 at 06:23
  • if u not get in the response [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; change this line into [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; – Anbu.Karthik Jun 04 '14 at 06:49
  • need more information follow this link http://www.microsoft.com/web/post/connecting-the-iphone-with-aspnet-web-pages – Anbu.Karthik Jun 04 '14 at 06:51
  • after the modification also same response :( –  Jun 04 '14 at 07:08
  • if u not get in the response [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; change this line into [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; – Anbu.Karthik Jun 04 '14 at 07:15