Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Username and password need to be encoded using Base64 encoding when using Basic HTTP authentication.</p> <p>From <a href="http://en.wikipedia.org/wiki/Basic_access_authentication" rel="nofollow">Wikipedia's Article on that subject</a>:</p> <blockquote> <p>Client side</p> <p>When the user agent wants to send the server authentication credentials it may use the Authorization header.</p> <p>The Authorization header is constructed as follows:[6] Username and password are combined into a string "username:password"</p> <p>The resulting string literal is then encoded using Base64</p> <p>The authorization method and a space i.e. "Basic " is then put before the encoded string. For example, if the user agent uses 'Aladin' as the username and 'sesam open' as the password then the header is formed as follows:</p> </blockquote> <pre><code>Authorization: Basic QWxhZGluOnNlc2FtIG9wZW4= </code></pre> <hr> <p>See this corrected code:</p> <pre><code>[...] NSString *str1 = [NSString stringWithFormat:@"%@:%@",userName,password]; NSString *encodedString = [self stringByBase64EncodingWithString:str1]; [request addValue:[NSString stringWithFormat:@"Basic %@",encodedString] forHTTPHeaderField:@"Authorization"]; [...] - (NSString *)stringByBase64EncodingWithString:(NSString *)inString { NSData *data = [NSData dataWithBytes:[inString UTF8String] length:[inString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; NSUInteger length = [data length]; NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; uint8_t *input = (uint8_t *)[data bytes]; uint8_t *output = (uint8_t *)[mutableData mutableBytes]; for (NSUInteger i = 0; i &lt; length; i += 3) { NSUInteger value = 0; for (NSUInteger j = i; j &lt; (i + 3); j++) { value &lt;&lt;= 8; if (j &lt; length) { value |= (0xFF &amp; input[j]); } } static uint8_t const base64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; NSUInteger idx = (i / 3) * 4; output[idx + 0] = base64EncodingTable[(value &gt;&gt; 18) &amp; 0x3F]; output[idx + 1] = base64EncodingTable[(value &gt;&gt; 12) &amp; 0x3F]; output[idx + 2] = (i + 1) &lt; length ? base64EncodingTable[(value &gt;&gt; 6) &amp; 0x3F] : '='; output[idx + 3] = (i + 2) &lt; length ? base64EncodingTable[(value &gt;&gt; 0) &amp; 0x3F] : '='; } return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload