Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

iphone - Pass object as a parameter to wcf service in Objective C on iOS

I have a wcf service which accepts a parameter like this:

[DataContract]
public class Person
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Family { get; set; }
}

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, UriTemplate = "")]
int InsertPerson(Person person);

I am familiar how to consume a wcf service with string parameter from Objective-C but in this case I want to pass an instance of Person class as parameter. How can I do that?

 NSString *path = [[NSString alloc] initWithFormat:@"http://192.168.0.217/JSON/Service1.svc/InsertPerson"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];

[request setHTTPMethod:@"POST"];  

[[NSURLConnection alloc] initWithRequest:request delegate:self];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

(From memory only here!)

Try setting the body of the request thus:

{ person: { ID: 1, Name: "Joe", Family: "Bloggs" } }

And then remember to send the Content-Type header as application/json, as well as making sure you use the POST method.

I'm sorry I can't actually give you the actual code for doing this - I'm not an objective-c developer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...