標籤:
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"123",@"longitude",@"30",@"latitude",@"上傳資料",@"message",nil];
//dictionaryToNSJoson
NSError *err=nil;
NSData *jasonData=[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&err];
NSString *jason=[[NSString alloc]initWithData:jasonData encoding:NSUTF8StringEncoding];
//u建立一個web路徑
NSString *[email protected]"你的url";
webUrl=[webUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:webUrl];
//建立一個帶協議緩衝類型的請求
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];
NSString *[email protected]"application/json";
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
//設定表單提交的方法(預設為get)
[request setHTTPMethod:@"post"];
//設定要提交的參數
[request setHTTPBody:[jason dataUsingEncoding:NSUTF8StringEncoding]];
NSData *recvData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if(recvData!=nil)
{
NSLog(@"傳值成功!!");
}
else
{
NSLog(@"傳值失敗!!");
}
iOS開發--http協議 post上傳資料