2014-10-31 102 views
1

我試圖從服務器獲取數據,但在服務器測試客戶端中運行相同的鏈接並獲得正確結果時,收到來自服務器的錯誤消息{"Message":"Wrong Process.","Success":false,"Cookie":null}。那麼請請將我的代碼放在哪裏?爲什麼我的NSMutableURLRequest失敗?

關鍵是data

值是<r_PM act=\"par\"/>

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/service/par.ashx"]]; 

    NSString *message = [[NSString alloc] initWithFormat:@"data=<r_PM act=\"par\"/>"]; 

    NSString *msgLength = [NSString stringWithFormat:@"%lu",(unsigned long)[message length]]; 

    [postRequest addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [postRequest addValue:msgLength       forHTTPHeaderField:@"Content-Length"]; 

    [postRequest setHTTPMethod:@"POST"]; 
    [postRequest setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSURLConnection * connection = [[NSURLConnection alloc] 
            initWithRequest:postRequest 
            delegate:self startImmediately:NO]; 

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

    NSURLResponse *response; 
    NSError *err; 

    responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&err]; 

    NSLog(@"responseData is: %@", responseData); 

    NSString* newStr = [[NSString alloc] initWithData:responseData encoding: ]; 

    NSLog(@"Results : %@", newStr); 

} 
+0

實際的錯誤信息是「我寫錯誤的事情」? – zaph 2014-10-31 13:02:36

+0

你爲什麼開始連接'[連接開始]'並執行'sendSynchronousRequest:'請求? – zaph 2014-10-31 13:05:12

+0

@Zaph它用不同的語言編寫,我會編輯它。對不起 – CAN 2014-10-31 13:05:19

回答

3

你正在創建其內容類型是XML,但其請求主體不是請求。 「data = ...」格式不是XML。在沒有提供有效請求的API文檔或示例代碼的情況下,我們很難爲您提供幫助。另外,您的Objective-C代碼會發出請求兩次,一次使用基於代理的NSURLConnection(您尚未共享您的代理實現),然後再次使用sendSynchronousRequest發出請求。只發布一次。如果你喜歡sendSynchronousRequest方法的簡單性,我建議你使用它的sendAsynchronousRequest兄弟,這也很方便,但不是同步的。


離線,通過檢查你是從另一個工具發送請求,我們確認,這個問題是

  • 請求application/x-www-form-urlencodedapplication/xml

    因此,更換Content-Type行:

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    
  • 數據必須是百分之逃脫,例如:

    NSString *value = @"<r_PM act=\"par\"/>"; 
    NSString *message = [NSString stringWithFormat:@"data=%@", [self percentEscapeString:value]]; 
    

    其中,百分比逸出常規可能是這樣的:

    - (NSString *)percentEscapeString:(NSString *)string 
    { 
        NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
                           (CFStringRef)string, 
                           (CFStringRef)@" ", 
                           (CFStringRef)@":/[email protected]!$&'()*+,;=", 
                           kCFStringEncodingUTF8)); 
        return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 
    } 
    
+0

因此,現在我已經刪除了'data',並將它保留爲'@「」&我刪除了'NSURLConnection'。我如何添加forKey?我將搜索sendAsynchronousRequest。 – CAN 2014-10-31 13:19:20

+0

或者你能幫助我嗎? – CAN 2014-10-31 13:28:36

+0

「for key」在XML請求(或至少不是這種格式)的上下文中沒有意義。除非我們更瞭解您的Web服務需要什麼格式,否則我們無法幫您創建有效的請求。向我們展示創建有效請求的示例代碼。或者分享鏈接到您的API文檔。 – Rob 2014-10-31 14:02:50