1

I'm sending a HTTP request to an ASP.NET web service and it returns the response as JSON (The web service originally returns a DataTable and it uses JSON.NET to convert it to JSON).

I'm getting the response as JSON. But with that an XML header comes along!

2013-02-21 16:42:41.062 HttpRequestTest[6023:c07] <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.my-comp.com/">[
  {
    "Int_Proj": 152,
    "NewMailCnt": 79,
    "DocsForRev": 2,
    "DocsForRel": 1,
    "Int_Sec": 2,
    "UnregCnt": 3225,
    "UnregAccss": 3,
    "OutstMLCnt": 276,
    "TBEdition": 1,
    "ProjNo": "TESTPROJ1",
    "ProjTitle": "TESTPROJ1 - DO NOT TOUCH TESTPROJ1 - DO NOT TOUCH TESTPROJ1",
    "ServerName": "ISURU",
    "ServerUrl": "ISURU/myserver/",
    "Int_Server": 1,
    "Int_Key": 2,
    "IsGblAdrBk": 0
  },
  {
    "Int_Proj": 160,
    "NewMailCnt": 11,
...

Here's the Objective-C code for calling the web service.

NSURL *url = [NSURL URLWithString:@"http://isuru/TBWebService/Session.asmx/GetUserProjectList?userID=dr&companyID=qas&password=david&errorMsg=''"];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                                cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                            timeoutInterval:30];
NSData *urlData;
NSURLResponse *response;
NSError *error;

urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                    returningResponse:&response
                                                error:&error];
NSString *returnedData = [[NSString alloc] initWithData:urlData
                                                   encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnedData);

And here's the C# code of the ASP.NET web service method.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Logon(string userID, string companyID, string password, string projNo)
{
    MySoftware.TestSoft.User user = new MySoftware.TestSoft.User();
    DataTable userTable = user.ListUserAccessProjects(userID, companyID, password, "", "");
    if (userTable.Rows.Count == 0)
    {
        string jsonRet = JsonConvert.SerializeObject("ERROR - Invalid User ID, Company ID or Password.", Formatting.Indented);
        return jsonRet;
    }

    DataRow[] projectEntries = userTable.Select(String.Format("ProjNo='{0}'", projNo));
    if(projectEntries.Length==0)
    {
        string jsonRet = JsonConvert.SerializeObject("ERROR - Invalid Project Number.", Formatting.Indented);
        return jsonRet;
    }

    int int_Project = Convert.ToInt32(projectEntries[0]["int_proj"]);

    MySoftware.TestSoft.Logon logon = new  MySoftware.TestSoft.Logon();
    try
    {
        bool isSameServer = false;
        string sessonKey = logon.Login(Convert.ToInt32(projectEntries[0]["int_Key"]), Convert.ToInt32(projectEntries[0]["Int_Server"]), userID, companyID, password, int_Project, out isSameServer);
        string json = JsonConvert.SerializeObject(sessonKey, Formatting.Indented);
        return json;
    }
    catch (ApplicationException ex)
    {
        string jsonRet = JsonConvert.SerializeObject("ERROR - " + ex.Message, Formatting.Indented);
        return jsonRet;
    }
}

This same issue has been asked here on SO and I tried everything mentioned there but the to no avail. Besides none of them was related to Objective-C.

How can I get the JSON response without the XML header?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.