如何从iOS向c#web服务发送数据

如何从iOS向c#web服务发送数据,c#,ios,web-services,ipad,rest,C#,Ios,Web Services,Ipad,Rest,我已经开发了一个c#rest web服务来与我的iPad应用程序进行通信,到目前为止,我使用它没有任何问题。这是一件非常简单的事情,到目前为止,有几个查询和GET方法对我来说非常有效。现在的问题是,我正试图将数据从我的应用程序发送到web服务,以插入到我的数据库中 NSString*query=[nsstringwithformat:@“http:mywebsite.com”]; _URL=[[NSURL alloc]initWithString:query]; NSMutableURLRequ

我已经开发了一个c#rest web服务来与我的iPad应用程序进行通信,到目前为止,我使用它没有任何问题。这是一件非常简单的事情,到目前为止,有几个查询和GET方法对我来说非常有效。现在的问题是,我正试图将数据从我的应用程序发送到web服务,以插入到我的数据库中

NSString*query=[nsstringwithformat:@“http:mywebsite.com”];
_URL=[[NSURL alloc]initWithString:query];
NSMutableURLRequest*request=[[NSMutableURLRequest alloc]initWithURL:[URL缓存策略:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0];
[请求设置HttpMethod:@“POST”];
[请求addValue:@“应用程序/json”用于HttpHeaderField:@“内容类型”];
[请求addValue:@“应用程序/json”用于HttpHeaderField:@“接受”];
NSData*requestData=[NSData dataWithBytes:[json UTF8String]长度:[json长度]];
[请求setHTTPBody:requestData];
NSURLConnection*connection=[[NSURLConnection alloc]initWithRequest:request委托:自启动中间:YES];
这基本上是我的objective-C代码,我尝试建立连接,将http方法设置为post并对要发送的NSData进行编码。这个NSData只是一个json字符串,已经可以使用了

现在在我的web服务中,我尝试使用字节[]获取这个NSData,并将其解码为字符串,获取json等等。问题是我做不到。我的web服务方法代码是:

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json,XmlSerializeString=false)]
公共布尔提交结果(字节[]数据){
String jsonString=System.Text.Encoding.UTF8.GetString(数据);
试一试{
JavaScriptSerializer json=新的JavaScriptSerializer();
var resultado=json.Deserialize(jsonString);
…然后继续。恢复所有这些:我无法将数据发送到web服务,因此我可以将其插入数据库。任何帮助都将不胜感激

顺便说一句,我得到的错误是这样可怕的事情:

{“Message”:“Type\u0027System.Collections.Generic.IDictionary`2[[System.String,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089],[System.Object,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]。\u0027不支持数组的反序列化。”,“StackTrace”:“位于System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList列表,类型类型,JavaScriptSerializer序列化程序,布尔throwOnError,IList&convertedList)\r\n位于System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal”(对象o,类型,JavaScriptSerializer序列化程序,布尔throwOnError,对象和转换对象)\r\n位于System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(对象o,类型,JavaScriptSerializer序列化程序,布尔throwOnError,对象和转换对象)\r\n位于System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer,字符串输入,类型,Int32 depthLimit)\r\n位于System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](字符串输入)\r\n位于System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData methodData)”,“异常类型”:“System.InvalidOperationException”}

所以,为了帮助你帮助我,这里是我的json:=)

更新:

经过大量研究,我认为这与我在Xcode中的序列化有关。它是这样的:

_arrayPerguntas = [[NSMutableArray alloc]init];
    for (int i = 0; i < _conteudoProgramatico.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:1]];
        [resp setPerguntaID:[_idConteudoProgramatico objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasConteudoProgramatico objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _sessoesPraticas.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:2]];
        [resp setPerguntaID:[_idSessoesPraticas objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasSessoesPraticas objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _materiaisSuporte.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:3]];
        [resp setPerguntaID:[_idMateriaisSuporte objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasMateriaisSuporte objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _apresentacao.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:4]];
        [resp setPerguntaID:[_idApresentacao objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasApresentacao objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _expectativas.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:5]];
        [resp setPerguntaID:[_idExpectativas objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasExpectativas objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _feedBack.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:6]];
        [resp setPerguntaID:[_idFeedback objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasFeedback objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    InqueritosResposta *resp = [[InqueritosResposta alloc]init];
    [resp setNb:[NSNumber numberWithInt:_nb]];
    [resp setCursoID:[self indiceSessao:_nomeSessao]];
    [resp setCategoriaID:[NSNumber numberWithInt:7]];
    [resp setPerguntaID:[NSNumber numberWithInt:22]];
    [resp setRespostaTexto:_respostaObservacoes];
    [_arrayPerguntas addObject:resp];

    NSError *writeError = nil;
    NSMutableArray *jsonRespostas = [[NSMutableArray alloc]init];
    for(InqueritosResposta *resp in _arrayPerguntas)
    {
        NSMutableDictionary *jAnswer = [[NSMutableDictionary alloc] init];
        [jAnswer setObject:resp.nb forKey:@"nb"];
        [jAnswer setObject:resp.cursoID forKey:@"cursoID"];
        [jAnswer setObject:resp.categoriaID forKey:@"categoriaID"];
        [jAnswer setObject:resp.perguntaID forKey:@"perguntaID"];
        if (resp.respostaTipo != nil) {
            [jAnswer setObject:resp.respostaTipo forKey:@"respostaTipo"];
        }
        if (resp.respostaTexto != nil) {
            [jAnswer setObject:resp.respostaTexto forKey:@"respostaTexto"];
        }
        [jsonRespostas addObject: jAnswer];
    }
    NSArray *final = [NSArray arrayWithArray:jsonRespostas];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:final options:NSJSONWritingPrettyPrinted error:&writeError];

    [self submitData:jsonData];
\u arrayPerguntas=[[NSMutableArray alloc]init];
对于(int i=0;i<\u conteudoProgramatico.count;i++){
InqueritosResposta*resp=[[InqueritosResposta alloc]init];
[resp setNb:[NSNumber NUMBER WITHINT:_nb]];
[resp SetCursoroid:[self indicatesSao:_nomessao];
[resp SetCategoriaaid:[NSNumber Number Withint:1]];
[resp setPerguntaID:[u idConteudoProgramatico objectAtIndex:i];
[resp setRespostaTipo:[自编码字符串:[[u respostasconteudoprogrammatico objectAtIndex:i]];
[_arrayperguntasaddobject:resp];
}
对于(int i=0;i<_sessosespraticas.count;i++){
InqueritosResposta*resp=[[InqueritosResposta alloc]init];
[resp setNb:[NSNumber NUMBER WITHINT:_nb]];
[resp SetCursoroid:[self indicatesSao:_nomessao];
[resp SetCategoriaaid:[NSNumber Number Withint:2]];
[resp setPerguntaID:[u idessosespraticas objectAtIndex:i];
[resp setRespostaTipo:[self codeToString:[u respostatassassosesparaticas objectAtIndex:i]];
[_arrayperguntasaddobject:resp];
}
对于(int i=0;i<\u materialisuporte.count;i++){
InqueritosResposta*resp=[[InqueritosResposta alloc]init];
[resp setNb:[NSNumber NUMBER WITHINT:_nb]];
[resp SetCursoroid:[self indicatesSao:_nomessao];
[resp SetCategoriaaid:[NSNumber Number Withint:3]];
[resp setPerguntaID:[u id materialisuporte objectAtIndex:i];
[resp setRespostaTipo:[self codeToString:[u respostasmaterialissupporte objectAtIndex:i]];
[_arrayperguntasaddobject:resp];
}
对于(int i=0;i<\u apresentacao.count;i++){
InqueritosResposta*resp=[[InqueritosResposta alloc]init];
[resp setNb:[NSNumber NUMBER WITHINT:_nb]];
[resp SetCursoroid:[self indicatesSao:_nomessao];
[resp SetCategoriaaid:[NSNumber Number Withint:4]];
[resp setPerguntaID:[u idApresentacao objectAtIndex:i];
[resp setRespostaTipo:[自编码字符串:[[u respostasapresentaco objectAtIndex:i]];
[_arrayperguntasaddobject:resp];
}
对于(int i=0;i<\u expectatias.count;i++){
InqueritosResposta*resp=[[InqueritosResposta alloc]init];
[resp setNb:[NSNumber NUMBER WITHINT:_nb]];
[resp SetCursoroid:[self indicatesSao:_nomessao];
[resp SetCategoriaaid:[NSNumber Number Withint:5]];
[resp setPerguntaID:[u ideexpectatias objectAtIndex:i];
[resp setRespostaTipo:[自编码字符串:[[u respostatespecta]
_arrayPerguntas = [[NSMutableArray alloc]init];
    for (int i = 0; i < _conteudoProgramatico.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:1]];
        [resp setPerguntaID:[_idConteudoProgramatico objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasConteudoProgramatico objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _sessoesPraticas.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:2]];
        [resp setPerguntaID:[_idSessoesPraticas objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasSessoesPraticas objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _materiaisSuporte.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:3]];
        [resp setPerguntaID:[_idMateriaisSuporte objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasMateriaisSuporte objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _apresentacao.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:4]];
        [resp setPerguntaID:[_idApresentacao objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasApresentacao objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _expectativas.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:5]];
        [resp setPerguntaID:[_idExpectativas objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasExpectativas objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _feedBack.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:6]];
        [resp setPerguntaID:[_idFeedback objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasFeedback objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    InqueritosResposta *resp = [[InqueritosResposta alloc]init];
    [resp setNb:[NSNumber numberWithInt:_nb]];
    [resp setCursoID:[self indiceSessao:_nomeSessao]];
    [resp setCategoriaID:[NSNumber numberWithInt:7]];
    [resp setPerguntaID:[NSNumber numberWithInt:22]];
    [resp setRespostaTexto:_respostaObservacoes];
    [_arrayPerguntas addObject:resp];

    NSError *writeError = nil;
    NSMutableArray *jsonRespostas = [[NSMutableArray alloc]init];
    for(InqueritosResposta *resp in _arrayPerguntas)
    {
        NSMutableDictionary *jAnswer = [[NSMutableDictionary alloc] init];
        [jAnswer setObject:resp.nb forKey:@"nb"];
        [jAnswer setObject:resp.cursoID forKey:@"cursoID"];
        [jAnswer setObject:resp.categoriaID forKey:@"categoriaID"];
        [jAnswer setObject:resp.perguntaID forKey:@"perguntaID"];
        if (resp.respostaTipo != nil) {
            [jAnswer setObject:resp.respostaTipo forKey:@"respostaTipo"];
        }
        if (resp.respostaTexto != nil) {
            [jAnswer setObject:resp.respostaTexto forKey:@"respostaTexto"];
        }
        [jsonRespostas addObject: jAnswer];
    }
    NSArray *final = [NSArray arrayWithArray:jsonRespostas];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:final options:NSJSONWritingPrettyPrinted error:&writeError];

    [self submitData:jsonData];