如何 post 特定数据格式以在 ios objective c 中休息 wcf json 服务

How to post particular data format to rest wcf json services in ios objective c

我需要 post 以下格式类型,并获取 json 数据

{
    "Authentication": {
      "Username": "testUser@123",
      "Password": "testPassword@123"
    },
    "RequestType": 4
  }

并且正在使用以下代码 post 到 wcf 网络服务,

    NSData *__jsonData = nil;
    NSString *__jsonString = nil;
    NSURL *url=[NSURL URLWithString:@"http://adservicetest.azurewebsites.net/order/"];


    dict = @{@"Authentication":@{@"Username":email.text,@"Password":password.text},@"FileID": [NSNumber numberWithInt:fileID],@"RequestType":[NSNumber numberWithInt:requestType]};

  if([NSJSONSerialization isValidJSONObject:dict])
    {
        __jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
      __jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding];
         NSLog(@"Error %@", __jsonString);
    }

    // Be sure to properly escape your url string.

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: __jsonData];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [__jsonData length]] forHTTPHeaderField:@"Content-Length"];

    NSError *errorReturned = nil;
    NSURLResponse *theResponse =[[NSURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];

    if (errorReturned)
    {
        NSLog(@"Error %@", errorReturned);
    }
    else
    {
        NSString *responseString=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@", responseString);
    }

但是我得到了空的 responseString 值,而且我已经使用 chrome 高级休息客户端测试了这个 post 方法它的工作,请更正我在做什么 wrong.am 无法解决这个问题从过去2天。请帮帮我。

谢谢

Please check below output and let me know your query resolved or not.

Please use below code and comment your code and then your can convert NSData to your required data format.

    NSString *responseString = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
    NSLog(@"strAddr = %@", responseString);

       // NSString *responseString=[NSJSONSerialization JSONObjectWithData:data options:nil error:&errorReturned];
       // NSLog(@"%@", responseString);
      //  NSLog(@"Error %@", errorReturned);

Printing description of responseString:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Service</p>
      <p xmlns="">Endpoint not found. Please see the <a rel="help-page" href="http://adservicetest.azurewebsites.net/help">service help page</a> for constructing valid requests to the service.</p>
    </div>
  </body>
</html>

请更新您的 NSURL 并让我知道更多问题:)

NSURL *url=[NSURL URLWithString:@"http://adservicetest.azurewebsites.net/order/json/process"];

响应将是:

Jason Data = {"Authentication":{"Password":"testPassword@123","Username":"testUser@123"},"RequestType":4}
2015-01-21 14:40:09.166 TestProject[2036:529966] strAddr = {"Errors":[],"StatusCode":200,"StatusMessage":"Get Orders List processed successfully","Files":[{"FileID":28,"Borrower":{"FirstName":"Ram","LastName":"Varma","PhoneNumbers":{"HomePhone":"5122154434","WorkPhone":"5124848340"},"Email":"ram@vonexpy.com"},"CoBorrower":{"FirstName":"Nadeem","LastName":"Shaik","PhoneNumbers":{"HomePhone":"5122154434","WorkPhone":"04065474405"},"Email":"nadeem@vonexpy.com"},"LenderDetails":{"LenderName":"VonExpy","LenderAddress":{"Address1":"101 ","Address2":"meghna ","City":"hyd","State":"AP","Zip":"500033"}},"LoanDetails":{"LoanType":"Housing","LoanPurpose":"business","LoanNumber":"VonExpy"},"PropertyAddress":{"Address1":"hyd","Address2":"guntur","City":"jubilee ","State":"UP","Zip":"87957"},"PropertyEstimatedValue":"fhy76","Orders":[{"OrderID":28,"AppraiserID":"27","AppraiserName":"FirstCLose","ProductDetails":{"ProductType":"hudy","FHACaseNumber":"746tr65"},"DueDate":"43543","InspectionDate":"983482374","Fee":"034202","NotesList":[],"Status":4,"Documents":[],"Condition":"987hkkk","ApplicationId":1,"ApplicationOrderId":"88"}]}]}