ASIHTTPRequest problem

2019-08-18 01:25发布

问题:

I have posted a question earlier but unfortunately I did not get any useful answer so I'll try in a different way. Here's the scenario.

I've a simple WCF RESTful service set up on a hosted environment.

I'm trying to communicate with this service from iphone 4. Here's what is working.

I have two uri template set up. One for GET and one for POST.

Running a console app on my local machine I'm able to GET and POST data. The data being extracted from the request and sent to a mysql database.

On the iphone I'm able to GET data.

Unrotunately I'm not able to POST data from the iphone.

I've been trying to make ASIHTTPResuest working. When I'm sending the request I get responseStatusCode 200 back.

I tried to create the request using NSmutableTableRequest but that didn't work either. Request was sent no error.

At this point I'm assuming that the POST request is correct as I'm not getting any error but somehow the webservice are not able to get the data out of the XML body.

I'm still in the middle of the WCF learning process so I got to miss something "basic" here.

Would this Datacontract and Operationcontact

[OperationContract] 
[WebInvoke(UriTemplate = "/create", 
Method = "POST", RequestFormat=WebMessageFormat.Xml, 
BodyStyle=WebMessageBodyStyle.Bare)] 
void CreateProduct(Product product);

....


[DataContract(Namespace="")]
public class Product { 

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

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

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

} 

be able to read this message?

NSURL *url = [NSURL URLWithString:@"http://www.mydomian.com/create"]; 
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
NSData *myPostData = [[NSString stringWithFormat:@"<Product><Description>desc1</Description><Id></Id><Name>somebody</Name></Product>"] dataUsingEncoding:NSUTF8StringEncoding];  
NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];  
[request setPostBody:myMutablePostData];  
[request setRequestMethod:@"POST"];  
[request addRequestHeader:@"Content-Type" value:@"application/xml"];  
[request setDelegate:self];  
[request startSynchronous]; 

Literally any help would be greatly appriciated.

回答1:

Using ASIHttpRequest is astoundingly simple, here's an example...

it will print out your return values, so you know what's happening. NO PROBLEM with your XML situation.

By the way, you should not use the whacky "synchronous" method. In practice, use asynchronous in all cases.

I strongly recommend you donate a few dollars to that bloke who makes ASIHttpRequest. There is a link right there on the ASIHttpRequest page. If he ever stops supporting ASIHttpRequest, we're all buggered. He is extremely polite and he generally answers email questions.

// how to use ASIHttpRequest...
NSURL *url = [NSURL URLWithString:@"https://www.blah.com/cgi-bin/blah.cgi?blah"];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request setPostValue:@"blah" forKey:@"blah"];
[request setPostValue:@"blah" forKey:@"blah"];

[request setDelegate:self];
[request setDidFinishSelector:@selector(allDone:)];
[request setDidFailSelector:@selector(allDoneProblem:)];

[request startAsynchronous];


-(void)allDone:(ASIHTTPRequest *)request
    {
    NSString *myResult = [request responseString];
    NSLog(@"I got this %@", myResult);
    return;
    }

Regarding your server side ... good luck :-/ Things were easier in the old days using perl or php, instead of all this new "easy" stuff that never works because people figure anyone can do it, you know, so it's often a shambles. It's tough. Can't help on that one. Hope the above helps. Open your console to see the results.

Incidentally here's the LINK for ASIHttpRequest, as a number of poeple have asked for the URL

http://allseeing-i.com/ASIHTTPRequest/



回答2:

For posting form data, you want to use the ASIHTTPRequest subclass ASIFormDataRequest.

It comes with a bunch of methods for setting post field values. Go to the ASIHTTPRequest "How to use it" page and search for ASIFormDataRequest, and you'll find tons of sample code.