Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net iOS 5 Json错误400在执行到WCF的POST服务时_.net_Ios_Xcode_Json_Wcf - Fatal编程技术网

.net iOS 5 Json错误400在执行到WCF的POST服务时

.net iOS 5 Json错误400在执行到WCF的POST服务时,.net,ios,xcode,json,wcf,.net,Ios,Xcode,Json,Wcf,get工作正常,能够使用测试winform应用程序发布到数据库,但在尝试从iOS应用程序发布时,从IIS日志中获取错误400。这是IIS日志2012-06-28 12:13:39 192.168.100.112 POST/JsonWcfService/GetEmployees.svc/json/updateuser-58129-192.168.100.231 WcfTest/1.0+CFNetwork/548.0.3+Darwin/11.4.0 400 0 4163 这是WCF服务代码

get工作正常,能够使用测试winform应用程序发布到数据库,但在尝试从iOS应用程序发布时,从IIS日志中获取错误400。这是IIS日志2012-06-28 12:13:39 192.168.100.112 POST/JsonWcfService/GetEmployees.svc/json/updateuser-58129-192.168.100.231 WcfTest/1.0+CFNetwork/548.0.3+Darwin/11.4.0 400 0 4163

这是WCF服务代码

    [OperationContract]
    [WebInvoke(Method = "POST",
       ResponseFormat = WebMessageFormat.Json,
       RequestFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.Wrapped,
       UriTemplate = "json/updateuser")]
    //method
     Employee PostEmp(Employee emp);    

[DataContract]
public class Employee
{
    [DataMember]
    public string firstname { get; set; }
    [DataMember]
    public string lastname { get; set; }
    [DataMember]
    public decimal salary { get; set; }
    [DataMember]
    public int idkey { get; set; }
    public Employee()
    {

    }
}

public Employee GetEmp(int IDKey)
{
    Employee emp = new Employee();

    using (EmpDBEntities empContext = new EmpDBEntities())
    {
        var j = (from t in empContext.EMPKeys where t.IDKey == IDKey select t).FirstOrDefault();
        emp = (new Employee(j.FirstName, j.LastName, j.Salary, j.IDKey));
        return emp;
    }
 }
xcode是从该站点上的另一个帖子中获取的

NSArray *keys = [NSArray arrayWithObjects:@"idkey", @"firstname", @"lastname",@"salary", nil];
NSArray *objects = [NSArray arrayWithObjects:@"1", @"jim", @"jones", @"450",nil];
NSData *__jsonData = nil;
NSString *__jsonString = nil;
NSURL *url = [NSURL
URLWithString:@"http://<ip address>/JsonWcfService/GetEmployees.svc/json/updateuser"];

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

if([NSJSONSerialization isValidJSONObject:jsonDictionary])
{
    __jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
    __jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding];
}

// 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) {
    // Handle error.
}
else
{
    NSError *jsonParsingError = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments
NSArray*keys=[NSArray数组,其对象为:@“idkey”、@“firstname”、@“lastname”、@“salary”、nil];
NSArray*objects=[NSArray阵列,其对象为:@“1”,“jim”,“jones”,“450”,无];
NSData*\uuu jsonData=nil;
NSString*\uuu jsonString=nil;
NSURL*url=[NSURL
URLWithString:@“http:///JsonWcfService/GetEmployees.svc/json/updateuser"];
NSDictionary*jsonDictionary=[NSDictionary Dictionary WithObjects:objects forKeys:keys];
if([NSJSONSerialization isValidJSONObject:jsonDictionary])
{
__jsonData=[NSJSONSerialization dataWithJSONObject:jsonDictionary选项:0错误:无];
__jsonString=[[NSString alloc]initWithData:\uu jsonData编码:NSUTF8StringEncoding];
}
//确保正确转义url字符串。
NSMutableURLRequest*请求=[NSMutableUrlRequestRequestWithURL:url];
[请求设置HttpMethod:@“POST”];
[请求setHTTPBody:uu jsonData];
[请求设置值:@“应用程序/json”用于HttpHeaderField:@“内容类型”];
[request setValue:[NSString stringWithFormat:@“%d”,HttpHeaderField:@“内容长度”];
n错误*errorReturned=nil;
NSURLResponse*响应=[[NSURLResponse alloc]init];
NSData*data=[NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
如果(返回错误){
//处理错误。
}
其他的
{
N错误*jsonParsingError=nil;
NSArray*jsonArray=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableContainers | NSJSONReadingAllowFragments
错误:&jsonParsingError];
}

问题在实施文件中更改,修改了两行,现在可以工作了

[OperationContract]
[WebInvoke(Method = "POST",
   ResponseFormat = WebMessageFormat.Json,
   RequestFormat = WebMessageFormat.Json,
   //BodyStyle = WebMessageBodyStyle.Wrapped, removed this line otherwise the Employee object was null
   //UriTemplate = "json/updateuser")
UriTemplate =""] //removed "json/updateuser and was able to post to the DB
//method
 Employee PostEmp(Employee emp);    

问题是在实现文件中修改了两行,现在可以工作了

[OperationContract]
[WebInvoke(Method = "POST",
   ResponseFormat = WebMessageFormat.Json,
   RequestFormat = WebMessageFormat.Json,
   //BodyStyle = WebMessageBodyStyle.Wrapped, removed this line otherwise the Employee object was null
   //UriTemplate = "json/updateuser")
UriTemplate =""] //removed "json/updateuser and was able to post to the DB
//method
 Employee PostEmp(Employee emp);