gpt4 book ai didi

iphone - 如何将带有用户名和密码的 JSON 对象发送到 iPhone 中的 RESET 登录 Web 服务?

转载 作者:行者123 更新时间:2023-12-03 20:52:26 27 4
gpt4 key购买 nike

我必须使用 JSON 对象发送用户名和密码才能登录 REST Web 服务。我已经在android中使用以下代码完成了。

HttpClient client = new DefaultHttpClient();
String responseBody;
JSONObject jObj = new JSONObject();

try
{
HttpPost post = new HttpPost("http://localhost:8080/MTA/login");
jObj.put("email", "user@email.com");
jObj.put("password", "password12345");

StringEntity se = new StringEntity(jObj.toString());

post.setEntity(se);
post.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setHeader("Content-type", "application/json");
System.out.println("webservice request executing");

ResponseHandler responseHandler = new BasicResponseHandler();
responseBody = client.execute(post, responseHandler);

System.out.println("Response : " + responseBody);
/*
* You can work here on your responseBody
* if it's a simple String or XML/JSON response
*/

}
catch(Exception e)
{
System.out.println("Exception : " + e);
}

我必须在 iOS 中做同样的事情。我在 iOS 中可以做什么?

最佳答案

我正在使用 Ge.tt API,为了登录,我正在执行以下操作:

NSString *email = @"Some email";
NSString *password = @"Some password"
NSString *apikey = @"some api key";
NSString *loginURL = @"http://open.ge.tt/1/users/login";

NSURL *url = [NSURL URLWithString:loginURL];

NSString *JSONString = [NSString stringWithFormat:@"{\"apikey\":\"%@\",\"email\":\"%@\",\"password\":\"%@\"}", apikey, email, password];

NSData *JSONBody = [JSONString dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] initWithURL:url];
loginRequest.HTTPMethod = @"POST";
loginRequest.HTTPBody = JSONBody;

NSOperationQueue *queue = [NSOperationQueue new];

[NSURLConnection sendAsynchronousRequest:loginRequest
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

// Manage the response here.

}];

关于iphone - 如何将带有用户名和密码的 JSON 对象发送到 iPhone 中的 RESET 登录 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10431243/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com