如何向服务器发出Json请求?

发布于 2025-01-05 12:24:30 字数 474 浏览 1 评论 0原文

我正在做一个应该与服务器一起工作的应用程序。

需要通过程序使用用户名和密码在服务器上进行授权。 我需要使用以下行向服务器发出请求:

{"login": "mad_fashist", "password": "eqeeuq313371", "method": "authentificator"}

该字符串必须位于杰森。

作为响应,如果身份验证失败,我应该得到一行:

{"validated": "false", "kuid": "", "sid": "", "uid": ""}

并且如果认证通过:

{"validated":"true","kuid":"6","sid":"834fe9b4626502bf9ff23485a408ac40","uid":"69"}

问题是如何发送和接收所有以上?

I'm doing an application that should work with the server.

Need to be authorized on the server through the program using the username and password.
I need to make a request to the server with the line:

{"login": "mad_fashist", "password": "eqeeuq313371", "method": "authentificator"}

The string must be in Json.

In response, I should get a line if authentication fails:

{"validated": "false", "kuid": "", "sid": "", "uid": ""}

And if authentication is passed:

{"validated":"true","kuid":"6","sid":"834fe9b4626502bf9ff23485a408ac40","uid":"69"}

The question is how to send and receive all of the above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撕心裂肺的伤痛 2025-01-12 12:24:30

使用SBJson框架。并执行以下操作:

-(void)requestProjects
{


    //I prepare the string

    NSString *preparedString=[NSString stringWithFormat:@"%@ %@", self.lastDate, self.currentCategory];
    NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:preparedString forKey:@"request"];

    //Prepare convert to json string
    NSString *jsonRequest = [jsonDict JSONRepresentation];

    NSLog(@"jsonRequest is %@", jsonRequest);

    //Set the URL YOU WILL PROVIDE

    NSURL *url = [NSURL URLWithString:@"http:xxxxxxxxxxxxxxxxxxx"];

    //PREPARE the request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    //Prepare data which will contain the json request string.
    NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

    //Set the propreties of the request 
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setValue:jsonRequest forHTTPHeaderField:@"Query-string"];
    //set the data prepared
    [request setHTTPBody: requestData];

    //Initialize the connection with request
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    //Start the connection
    [delegate showIndicator];
    [connection start];

}


//delegate methods:

//METHODS TO HANßDLE RESPONSE
#pragma mark NSURLConnection delegate methods
//WHen receiving the response
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@" Did receive respone");
    [responseData setLength:0];


}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    //While receiving the response data
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    //When failed just log
     [delegate hideIndicator];
    NSLog(@"Connection failed!");
    NSLog(@"Error %@", error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    //When the response data is downloaded 
   // NSLog(@" Data obtained %@", responseData);
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
   // NSLog(@" Response String %@", responseString);
    //converted response json string to a simple NSdictionary
    //If the response string is really JSONABLE I will have the data u sent me displayed succefully

    NSMutableArray *results = [responseString JSONValue];
    NSLog(@"Response: %@", results);
/// la la al alal alaa
}

您的后端应该定义 Json 中的响应的样子

Use SBJson framework. and do smth like:

-(void)requestProjects
{


    //I prepare the string

    NSString *preparedString=[NSString stringWithFormat:@"%@ %@", self.lastDate, self.currentCategory];
    NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:preparedString forKey:@"request"];

    //Prepare convert to json string
    NSString *jsonRequest = [jsonDict JSONRepresentation];

    NSLog(@"jsonRequest is %@", jsonRequest);

    //Set the URL YOU WILL PROVIDE

    NSURL *url = [NSURL URLWithString:@"http:xxxxxxxxxxxxxxxxxxx"];

    //PREPARE the request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    //Prepare data which will contain the json request string.
    NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

    //Set the propreties of the request 
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setValue:jsonRequest forHTTPHeaderField:@"Query-string"];
    //set the data prepared
    [request setHTTPBody: requestData];

    //Initialize the connection with request
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    //Start the connection
    [delegate showIndicator];
    [connection start];

}


//delegate methods:

//METHODS TO HANßDLE RESPONSE
#pragma mark NSURLConnection delegate methods
//WHen receiving the response
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@" Did receive respone");
    [responseData setLength:0];


}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    //While receiving the response data
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    //When failed just log
     [delegate hideIndicator];
    NSLog(@"Connection failed!");
    NSLog(@"Error %@", error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    //When the response data is downloaded 
   // NSLog(@" Data obtained %@", responseData);
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
   // NSLog(@" Response String %@", responseString);
    //converted response json string to a simple NSdictionary
    //If the response string is really JSONABLE I will have the data u sent me displayed succefully

    NSMutableArray *results = [responseString JSONValue];
    NSLog(@"Response: %@", results);
/// la la al alal alaa
}

It's your back end who should define how the response in Json will look like

燕归巢 2025-01-12 12:24:30

请参阅 NSJsonSerialization 类。这是ios5.0中新包含的。这是满足您需求的最灵活的一种。您可以在苹果开发者网站中访问它。
链接到 NSJSONSerialization

Refer NSJsonSerialization class. Which is newly included in ios5.0. which is the most flexible one for your need. you can access it in apple developer site.
link to NSJSONSerialization

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文