2012-02-20 33 views
0

我試圖通過HTTP GET請求從PC上的服務獲取答案(圖片)。 如果我把請求放入網頁瀏覽器,我會得到請求的圖片。如果我試圖在iPhone應用程序中獲得它,它不起作用。http GET上的ios

的要求是:

http://192.168.151.82:54000/snapshot?s=<snapshotrequest xmlns=\"http://www.vizrt.com/snapshotrequest\"><videomoderequest><width>880</width><height>495</height></videomoderequest><snapshotdata view=\"all\"/></snapshotrequest>&p=http://192.168.151.82:8580/element_collection/storage/shows/%%257B3646FFAC-4E77-41AB-BDFC-F581D157ABA3%%257D/elements/1000/ 

我爲獲得代碼:

NSString *str = [NSString stringWithFormat: @"http://192.168.151.82:54000/snapshot?s=<snapshotrequest xmlns=\"http://www.vizrt.com/snapshotrequest\"><videomoderequest><width>880</width><height>495</height></videomoderequest><snapshotdata view=\"all\"/></snapshotrequest>&p=http://192.168.151.82:8580/element_collection/storage/shows/%%257B3646FFAC-4E77-41AB-BDFC-F581D157ABA3%%257D/elements/1000/"]; 
NSURL *url = [[NSURL alloc] initWithString:str]; 
UIImage *img = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: url ] ]; 

可以看到,像報價和percentes是speciel字符handeled。 我正在使用wireshark在PC上觀看網絡通信,並且沒有任何通信。

+0

192.168.151.82 :54000可從外部訪問?看起來像一個本地ip地址,iphone無法到達。 – Prine 2012-02-20 13:36:54

+0

我同意prine。確保您正在通過無線連接,並且PC上的防火牆未阻止該請求。 – 2012-02-20 13:45:19

+0

網絡,IP和防火牆都可以。我可以在網絡瀏覽器中從我的Mac上請求圖像。 – gbaor 2012-02-20 13:57:14

回答

1

您需要的URL從他們創建一個URL

NSString * unencodeParameter = @"<snapshotrequest xmlns=\"http://www.vizrt.com/snapshotrequest\"><videomoderequest><width>880</width><height>495</height></videomoderequest><snapshotdata view=\"all\"/></snapshotrequest>&p=http://192.168.151.82:8580/element_collection/storage/shows/%%257B3646FFAC-4E77-41AB-BDFC-F581D157ABA3%%257D/elements/1000/"; 

NSString * encodedParameter = (NSString *)CFURLCreateStringByAddingPercentEscapes(
      NULL, 
      (CFStringRef)unencodedParameter, 
      NULL, 
      (CFStringRef)@"!*'();:@&=+$,/?%#[]", 
      kCFStringEncodingUTF8); 

NSString *str = [NSString stringWithFormat: @"http://192.168.151.82:54000/snapshot?s=%@",encodedParameter]; 
NSURL *url = [[NSURL alloc] initWithString:str]; 
UIImage *img = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: url ] ]; 

之前編碼您的參數,並且確保你的iPhone是在相同的WiFi您的計算機使用本地IP地址

+0

終於我可以看到通信,但服務器返回此錯誤: 提供的數據不足以允許PreviewServer決定場景渲染。 snapshotservice的有效載荷和場景參數都是null/empty。 gbaor 2012-02-20 14:39:05

+0

謝謝Saurabh!我不得不從percentescapes刪除字符:「=」和「&」,它正在工作!所以我現在(CFStringRef)@「!*'();:@ + $,/?%#[]」 – gbaor 2012-02-21 08:52:16