0

我需要将信息从应用程序传递到服务器,特别是文本格式的 a 和 b。这是 WCF 服务的代码:

public class iOSService 
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFo rmat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.Outgoin gResponse.ContentType = "text/xml";
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}

// Add more operations here and mark them with [OperationContract]


[OperationContract]
public string iOSTest2(string a, string b)
{
string res = "";
try
{
res=(int.Parse(a) + int.Parse(b)).ToString();
}
catch (Exception exp)
{
res = "Not a number";
}
return res;
}


}

服务器接收到 a 和 b 后,将它们相加,并返回它们的总和。

这是我将参数发送到 iOS 应用程序服务器的代码:

- (IBAction)test:(id)sender {

    NSArray *propertyNames = [NSArray arrayWithObjects:@"23", @"342", nil];
    NSArray *propertyValues = [NSArray arrayWithObjects:@"a", @"b",  nil];

    NSDictionary *properties = [NSDictionary dictionaryWithObjects:propertyNames forKeys:propertyValues];

    NSMutableArray  * arr;

    arr=[[NSMutableArray alloc]initWithObjects:properties, nil];
    NSLog(@"%@",arr);

    NSError * error;
    NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mathforyou.net/iOSservice.svc/iOSTest2"]];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];

    [request setValue:@"appliction/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:jsonData2];
    NSLog(@"JSON String: %@",jsonString);
    NSError *errorReturned = nil;
    NSURLResponse *theResponse =[[NSURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
    if (errorReturned) {
        //...handle the error
        NSLog(@"error");
    }
    else {
        NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"%@",retVal);
    }
}

但是服务器回复了我以下消息:

"ExceptionDetail":null,"ExceptionType":null,"Message":"The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.","StackTrace":null

这是来自 Xcode 的日志:

2013-03-01 17:38:28.657 2231233122[3442:c07] (
        {
        a = 23;
        b = 342;
    }
)
2013-03-01 17:38:28.659 2231233122[3442:c07] JSON String: [
  {
    "a" : "23",
    "b" : "342"
  }
]
2013-03-01 17:38:29.054 2231233122[3442:c07] {"ExceptionDetail":null,"ExceptionType":null,"Message":"The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.","StackTrace":null}

PS我请朋友帮助我,但由于他不知道objective-c为C++编写了一个程序并且代码有效,所以问题不在服务器上。这是代码:

string data = "{\"a\":\"560\",\"b\":\"90\"}";

byte[] bd = Encoding.UTF8.GetBytes(data);

HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("http://www.mathforyou.net/IOSService.svc/iOSTest2");
wr.ContentType = "application/json";
wr.Method = "POST";
wr.ContentLength = bd.Length;
Stream sw = wr.GetRequestStream();
sw.Write(bd, 0, bd.Length);
sw.Close();
HttpWebResponse resp = (HttpWebResponse)wr.GetResponse();
Stream s = resp.GetResponseStream();
byte[] bres = new byte[resp.ContentLength];
s.Read(bres, 0, bres.Length);
string ans = Encoding.UTF8.GetString(bres);
Console.WriteLine(ans);

请帮忙,我累坏了。

4

2 回答 2

2

如果您复制并粘贴了代码,看起来您可能拼错了您的Content-Type值。它应该是application/json,你有它作为appliction/json

另外,我不确定这是否重要,但您也没有明确设置内容长度。我不确定这是否setHTTPBody:适合你。

于 2013-03-01T18:25:29.000 回答
0

我不确定您是否仍然需要这个,但请尝试以下操作:

[request addValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:jsonString forHTTPHeaderField:@"json"]; //<-- I added this line
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonData2];
于 2013-05-14T08:08:25.430 回答