我需要将信息从应用程序传递到服务器,特别是文本格式的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}
P.S。我请了一位朋友帮助我,但由于他不知道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);
请帮助,我已经疲惫不堪。
答案 0 :(得分:2)
如果您复制并粘贴了代码,则表明您的Content-Type
值可能拼写错误。它应为application/json
,您将其作为appliction/json
。
此外,我不确定这是否重要,但您也没有明确设置内容长度。我不确定setHTTPBody:
是否适合您。
答案 1 :(得分: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];