我試圖從服務器獲取數據,但在服務器測試客戶端中運行相同的鏈接並獲得正確結果時,收到來自服務器的錯誤消息{"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);
}
實際的錯誤信息是「我寫錯誤的事情」? – zaph 2014-10-31 13:02:36
你爲什麼開始連接'[連接開始]'並執行'sendSynchronousRequest:'請求? – zaph 2014-10-31 13:05:12
@Zaph它用不同的語言編寫,我會編輯它。對不起 – CAN 2014-10-31 13:05:19