如何使用基本的http post和xcode进行身份验证?

时间:2010-11-26 23:13:18

标签: objective-c authentication http-post

我的建议是登录网站以获取我的个人资料信息 我有这段代码:

NSString *post = [NSString stringWithFormat:@"username=Mike&passwd=whatever"]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];


NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.myweb.com/profile"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];  

NSURLResponse *response=nil;
NSData *data;  
data= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];

NSString *content1 = [[NSString alloc]  initWithBytes:[data bytes]
                                              length:[data length] encoding: NSASCIIStringEncoding];
NSLog(@"Page = %@", content1);`

问题:

  1. 我从数据中得到的是登录信息 网络一遍又一遍,怎么了?该 登录不起作用!

  2. 我已将post变量命名为username和passwd,因为这是他们在html代码中的名称(标记)。这可以吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

您确定HTTP不应该是HTTPS吗?

根据给定的信息,很难给出答案。也许你澄清一下。

1)我假设“1 - 我从数据中获得的是一遍又一遍的登录网络,出了什么问题?”意味着你得到一个网页。作为对帖子或获取请求的回复,您将获得服务器将发送给您的内容。与浏览器相同。

2)是的。

答案 1 :(得分:1)

我正在尝试登录joomla网站:www.ambrosden.biz/administrator是的,回复是对的,如果我尝试使用错误的用户名/通行证登录,我会一遍又一遍地登录页面。为什么它不登录????顺便说一下,万一是https,我应该做些什么改变?谢谢 我不允许发布日志,因为它有超链接,我已截取屏幕截图,但显然我不允许上传图像。

这是删除某些链接以避免此限制的输出:

Page = <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                 ambrosden - 管理

       function setFocus(){         document.login.username.select();         document.login.username.focus();     }

                                           ambrosden                                                                                                                                                                            

                <h1>Joomla! Administration Login</h1>

                        <div id="section-box">
        <div class="t">
            <div class="t">
                <div class="t"></div>
            </div>
        </div>
        <div class="m">
            <form action="index.php" method="post" name="login" id="form-login" style="clear: both;">
<p id="form-login-username">
    <label for="modlgn_username">Username</label>
    <input name="username" id="modlgn_username" type="text" class="inputbox" size="15" />
</p>

<p id="form-login-password">
    <label for="modlgn_passwd">Password</label>
    <input name="passwd" id="modlgn_passwd" type="password" class="inputbox" size="15" />
</p>
    <p id="form-login-lang" style="clear: both;">
    <label for="lang">Language</label>
    <select name="lang" id="lang"  class="inputbox"><option value=""  selected="selected">Default</option><option value="en-GB" >English (United Kingdom)</option></select> </p>
<div class="button_holder">
<div class="button1">
    <div class="next">
        <a onclick="login.submit();">
            Login</a>

    </div>
</div>
</div>
<div class="clr"></div>
<input type="submit" style="border: 0; padding: 0; margin: 0; width: 0px; height: 0px;" value="Login" />
<input type="hidden" name="option" value="com_login" />
<input type="hidden" name="task" value="login" />
<input type="hidden" name="672372a5278b2ba6d345ec69f792c547" value="1" /></form>
            <div class="clr"></div>
        </div>
        <div class="b">
            <div class="b">
                <div class="b"></div>
            </div>
        </div>
    </div>

                <p>Use a valid username and password to gain access to the Administrator Back-end.</p>
                <p>
                    Return to site Home Page</a>
                </p>
                <div id="lock"></div>
                <div class="clr"></div>
            </div>
            <div class="b">
                <div class="b">
                    <div class="b"></div>
                </div>
            </div>
        </div>
        <noscript>
            Warning! JavaScript must be enabled for proper operation of the Administrator back-end.         </noscript>
        <div class="clr"></div>
    </div>
</div>
<div id="border-bottom"><div><div></div></div>