-5

可能重复:
iPhone SDK Xcode 4.2 iOS 5-如何将 json 发送到 url?(POST 不是 GET)[已解决]

    NSDictionary *requestDic=[NSDictionary dictionaryWithObjects:values forKeys:keys];
    NSString *jsonreq=[requestDic JSONRepresentation];
    NSURL *requrl=[NSURL URLWithString:[NSString stringWithFormat:@"%@index.php?r=%@&te=%@&noCheck=1&checkInstall=start",[Info serverUrl],[dictParameters objectForKey:@"r"],[dictParameters objectForKey:@"te"]]];
    requrl = [NSURL URLWithString:[[requrl absoluteString] stringByAppendingString:subUrl]];
    NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:requrl];
    [req setHTTPMethod:@"POST"];
    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [req setValue:@"application/json-x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    NSData *requestData=[NSData dataWithBytes:[jsonreq UTF8String] length:[jsonreq length]];
    [req setHTTPBody:requestData];
    [browser loadRequest:req]
4

3 回答 3

1

Dilshand 的回答是好的,但您可以轻松使用 ASIFormDataRequest。喜欢关注

 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setPostValue:mobileApplicationId forKey:@"mobileApplicationId"];
[request setPostValue:mobileApplicationVersion forKey:@"mobileApplicationVersion"];
[request setPostValue:userID forKey:@"userId"];
[request setPostValue:pass forKey:@"password"];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request setTimeOutSeconds:60];
[request startAsynchronous];
于 2012-05-23T13:46:12.413 回答
0

您要发布服务器的内容提供完整的详细信息以及您遇到的错误

尝试按照我已经使用它将数据发送到服务器,它对我来说很好,希望这会有所帮助

    NSString *post =[[NSString alloc] initWithFormat:@"ProviderNPI=%@&PatientID=%@&FileURL=%@&FileTYPE=%@&DataSynID=%@& AppointmentListingsID=%@",providerNPI,patientID,FILEURL,FILETYPE,dataSynID,appointmentListingID];


    NSURL *url=[NSURL URLWithString:@"http://myserver.com/Sync.php?"];

    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/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];


    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",data);
于 2012-04-06T07:43:46.437 回答
0

NSURL *url = [NSURL URLWithString:@"http://www.invoicera.com/app/api/check_json_api.php?token=7B92C122473A3D6F54E60D20AC5526D0"];

NSString *jsonRequest = [NSString stringWithFormat:@"&json_data=%@",[[NSString stringWithFormat:@"{\"listInvoice\":{\"client_id\":\"\",\"date_from\":\"\",\"date_to\":\"\",\"invoice_number\":\"\",\"invoice_record_status\":\"\",\"invoice_status\":\"\",\"page\":\"1\",\"per_page_record\":\"20\"}}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSLog(@"%@",jsonRequest);

NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

NSLog(@"%@",json_data);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];


[request setHTTPMethod:@"POST"];
[request setHTTPBody: json_data];
NSLog(@"%@",json_data);
// [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [json_data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
                      dataUsingEncoding:NSUTF8StringEncoding 
                      allowLossyConversion:YES]];

// [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];

NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]
                                  initWithRequest:request 
                                  delegate:self];

// Successful connection.
if (nsUrlConnection) {

   // [self initSpinner];
   // [self spinBegin];

    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData=data;
    [data release];
} 
// Unsuccessful connection.
else {

}  
// Clean up
[url release];
[request release];
// Close keypad.
于 2012-04-06T08:00:49.667 回答