1

我不太熟悉 iOS 7,我正在尝试构建一个需要设置值并将 HTTP 请求标头发送到我的服务器的应用程序。但是,似乎我的服务器确实收到了我发送的值,这是我从 FacebookSDK 获得的令牌(我已经使用 FacebookSDK 登录,现在我拿到了令牌),因为我收到了通知服务器没有将其视为 HTTPReques 标头。这是我的代码:

- (void)postToken
{
// Get the token from Facebook
NSString *token = FBSession.activeSession.accessTokenData.accessToken;

// Create the server name
NSMutableString *server = [[NSMutableString alloc] init];
[server appendString:@"*My server website here*"];

// URL Request and initialization
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:server]];

[postRequest setValue:token forHTTPHeaderField:@"Token"];

// Set the request's content type
[postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[postRequest setValue:token forHTTPHeaderField:@"Token"];

// Designate the request a POST request and specify its body data
[postRequest setHTTPMethod:@"POST"];

// The delegate provides receives notification
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:mutableReq delegate:self];

self.connection = connection;

[self.connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[self.connection start];

if (self.connection) // if connect successfully
{
    NSLog(@"Connection Success!");
    NSURL *url = [NSURL URLWithString:server]; // Initialize url
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // Initialize request
    NSURLResponse *server_response = [[NSURLResponse alloc] init]; // Initialize response

    self.receivedData = [[NSMutableData alloc] init];
    [self.receivedData appendData:[NSURLConnection sendSynchronousRequest:request returningResponse:&server_response error:nil]]; // Receive data synchronously
    [self connectionDidFinishLoading:self.connection];
}
else
    NSLog(@"Connection Failed!");
}

输出是“连接成功!” 并且输出包含我的令牌(所以我不想在这里发布)和来自服务器的消息告诉我没有提供令牌,这意味着它可以与服务器通信但服务器没有将令牌识别为标头 HTTP 请求。有人可以帮我解决这个问题吗?提前致谢!

4

0 回答 0