1

CocoaHTTPServer用来托管我自己的HTML文件。在index.html我试图对另一个资源执行请求但我无法成功执行该资源。这是我目前的设置。

 self.httpServer = [[HTTPServer alloc] init];
[self.httpServer setType:@"_http._tcp."];
[self.httpServer setPort:8090];
NSString *webPath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"Web"];
[self.httpServer setDocumentRoot:webPath];

然后在成功http://localhost:8090提供我的index.html文件时从浏览器加载。

然后我通过调用我的函数$(document).ready创建一个请求。AJAXstartPoll

proxyAjax: function ( opts ) {

    var proxyOpts = _.cloneDeep(opts);

    if (!proxyOpts.headers) proxyOpts.headers = {};
    proxyOpts.headers['x-forwarded-url'] = proxyOpts.url;
    proxyOpts.url = new URI({
      protocol: 'http',
      hostname: 'localhost',
      port: 8090,
      path: '/'
    }).toString();

    proxyOpts.type = proxyOpts.type || proxyOpts.method;

    console.log("PROXYING:", proxyOpts);
    return $.ajaxDigest(proxyOpts);
  },

// Attempt an API call to the camera on a short timeout.
// Call the callback when we have a non-timeout response.
startPoll: function ( callback ) {
    var self = this;
    console.log("Polling...");
    this.proxyAjax({
      url: 'http://192.168.123.1/deviceinfo',
      username: 'admin',
      password: 'password',
      timeout: 2000
    })
    .done( function ( data, textStatus, jqXHR ) {
      console.log("Poll success:", arguments);
      callback(jqXHR);
    })
  }

然后我在这个方法HTTPServer中获取请求HTTPConnection.m- (void)replyToHTTPRequest

Xcode 控制台显示了这个请求。

Received HTTP request:
GET / HTTP/1.1
Host: localhost:8090
X-Requested-With: XMLHttpRequest
Accept-Language: en-US,en;q=0.8
x-forwarded-url: http://192.168.123.1/deviceinfo
Accept: */*
User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5
Referer: http://localhost:8090/
Accept-Encoding: gzip, deflate, sdch
Connection: keep-alive

现在我尝试向 发出新请求x-forwarded-url并将该响应作为原始响应返回,但未成功。我的done回调startPoll永远不会被调用。

if ([[request headerField:@"x-forwarded-url"] length] > 0) {
    NSURL *url = [[NSURL alloc] initWithString:[request headerField:@"x-forwarded-url"]];
    HTTPMessage *remoteRequest = [[HTTPMessage alloc] initRequestWithMethod:@"HEAD" URL:url version:[request version]];
    [remoteRequest setHeaderField:@"Access-Control-Allow-Origin" value:@"*"];
    [remoteRequest setHeaderField:@"Access-Control-Allow-Methods" value:@"GET,PUT,POST,DELETE,OPTION"];
    [remoteRequest setHeaderField:@"Access-Control-Allow-Headers" value:@"Accept, Origin, Content-Type"];
    [remoteRequest setHeaderField:@"HOST" value:url.host];
    NSData *requestData = [remoteRequest messageData];
    NSString *responseString = [[NSString alloc] initWithData:requestData encoding:NSUTF8StringEncoding];
    APILogVerbose(@"%@[%p]: HTTP Request:\n%@", THIS_FILE, self, responseString);
    [asyncSocket writeData:requestData withTimeout:TIMEOUT_WRITE_HEAD tag:HTTP_REQUEST_HEADER];
    return;
}

有人看到我遗漏或应该尝试的任何东西吗?

4

1 回答 1

0

我希望看到"Access-Control-Allow-Origin"来自您的 HTTP 服务器的响应标头,而不是从您的服务器到 x-forwarded-url 的请求标头。

请参阅:Access-Control-Allow-Origin 标头如何工作?

于 2016-02-09T22:06:31.447 回答