自动登录.do网​​站(xcode)

时间:2015-03-10 22:29:06

标签: ios iphone xcode ipad

我正在尝试通过将用户名和密码传递到网址来找到自动登录此网站的方法,但我无法让它工作。

网址为:https://egov.uscis.gov/casestatus/displayLogon.do

我试过了:https://egov.uscis.gov/casestatus/displayLogon.do?username=myUsername&password=secretPassword

但它不起作用。有什么想法吗?

我试过这个,但没有运气:

NSString *Url = @"https://egov.uscis.gov/casestatus/logon.do";

    NSString *username = @"username";
    NSString *password = @"password";

    NSString *soapMessage = [NSString stringWithFormat:@"username=%@&password=%@", username, password];

    NSURL *url = [NSURL URLWithString:Url];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

--- ---更新 这是我现在的位置,仍然无法登录。

password = [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
username = [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
namePassword = [namePassword stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
nameUsername = [nameUsername stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:@"https://egov.uscis.gov/casestatus/logon.do"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

NSString *postData = [NSString stringWithFormat:@"%@=%@&%@=%@", nameUsername, username, namePassword, password];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = connection;

[connection start];

2 个答案:

答案 0 :(得分:0)

与网站上的服务器通信时,有一些选项。要登录,您可以使用(作为服务器上的开发人员)“POST”和“GET”。你在做什么是GET方法。您可以通过输入一些凭据并尝试登录来立即知道这是否是登录方法。您将看到字段显示在地址栏中(您甚至不必获得正确的凭据)。

所以最有可能是POST。这样,您可以通过首先不可见的字段或“普通”用户将凭据发送到服务器。有一些免费的东西可以看到发送POST请求时将使用的名称。您可以在firefox中点击f12(对于其他浏览器来说类似)并查看所有HTML。你确实可以看到他们使用“密码”和“用户名”作为字段名称(虽然这不是必需的!)。您必须将您的用户名和密码POST到这些字段才能使其正常工作。

基于你的标签,我假设您希望使用objective-c或Swift在Xcode中实现这一点。对于objective-c,您可以搜索“NSURLconnection”或更新的“NSURLSession”。

你可以阅读很多内容,但要开始阅读这篇文章很好:http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/。但你几乎可以尝试任何教程。

答案 1 :(得分:0)

您似乎喜欢使用GET,而Web服务则设计为使用POST方法。 您可以通过将整行粘贴到Web浏览器地址栏来简单地检查Web服务是GET还是POST。 或者,您可以使用Chrome Extension Postman检查正常运行的Web服务和测试参数。

根据我使用SOAP的经验,它将SOAP消息作为帖子正文发送,您的代码似乎会像下面一样进行更改;

NSString *Url = @"https://egov.uscis.gov/casestatus/logon.do";

NSString *username = @"username";
NSString *password = @"password";

NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\"?>
    <soap:Envelope
    xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\"
    soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">
    <soap:Body xmlns:m=\"http://www.example.org/logon.do\">
    <m:logon.do>
    <m:username>%@</m:username>
    <m:password>%@</m:password>
    </m:logon.do>
    </soap:Body>
    </soap:Envelope>", username, password];

NSURL *url = [NSURL URLWithString:Url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)         [soapMessage length]];

[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

肥皂消息模板不正确,但您需要发送整个肥皂消息包装参数,而不仅仅是参数。