解决方案 »
- UITabBarController 代码中延迟加载视图
- Unknown class pro_exam1ViewController in Interface Builder file.
- Iphone5S 体验(视频+截图)
- ios两个程序融合
- ios怎么把解析好的天气json数据录入到sqilte数据库中
- ios TabBarView 模拟器显示全黑
- 请问tab bar controller的多个tab页里面的公共信息怎么显示?
- iOS 限制输入特殊字符
- swift怎么和c c++一起用
- UIEdgeInsetsMake 失效了
- 在看一个ios项目的时候在搜索栏输入项目中存在的图片名居然搜索不到,为什么?难道设置权限了还是搜索方法有错误?求大神指点
- curl curl_easy_setopt 执行崩溃,请指教。。。
{
NSString *str = @"http://paiyuanxian.iushare.com:8080/paiyuanxian/mainQuery.do";
NSBundle*bundle=[[NSBundle mainBundle]init];
NSDictionary*dic=[NSDictionary dictionaryWithDictionary:[[[NSDictionary alloc]initWithContentsOfFile:[bundle pathForResource:@"urlArgument" ofType:@"plist"]]objectForKey:@"allFilmNews"]];
NSString *urlstr = [[NSString stringWithFormat:@"%@",str]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSLog(@"%@",urlstr);
NSURL *url = [NSURL URLWithString:urlstr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:1000];
[request addValue:@"{\"device\":\"Unknown iPhone\",\"language\":\"zh-Hans\",\"screenWidth\":640,\"systemName\":\"iOS\",\"appVersion\":\"5.0.2\",\"appName\":\"paiyuanxian\",\"screenHeight\":1136,\"deviceId\":\"08b88b9f8a9375ea786b80dbe1374e62\",\"systemVersion\":\"7.1\"}" forHTTPHeaderField:@"User-Agent"];
[request addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString *sBody = @"";
for (NSString *key in dic.allKeys) {
NSString *value = [dic objectForKey:key];
if (sBody.length>0) {
sBody = [sBody stringByAppendingString:@"&"];
}
sBody = [sBody stringByAppendingFormat:@"%@=%@", key, value];
}
NSLog(@"request = %@==========",request);
[request setHTTPBody: [sBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *urlResponse;
NSData *as = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:nil];
// NSLog(@"=====%d",as.length);
NSError *error;
NSDictionary *endDic = [NSJSONSerialization JSONObjectWithData:as options:kNilOptions error:&error];
NSLog(@"%@",endDic);}
{
NSString *str = @"http://paiyuanxian.iushare.com:8080/paiyuanxian/mainQuery.do";
NSBundle*bundle=[[NSBundle mainBundle]init];
NSDictionary*dic=[NSDictionary dictionaryWithDictionary:[[[NSDictionary alloc]initWithContentsOfFile:[bundle pathForResource:@"urlArgument" ofType:@"plist"]]objectForKey:@"allFilmNews"]];
NSString *urlstr = [[NSString stringWithFormat:@"%@",str]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSLog(@"%@",urlstr);
NSURL *url = [NSURL URLWithString:urlstr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:1000];
[request addValue:@"{\"device\":\"Unknown iPhone\",\"language\":\"zh-Hans\",\"screenWidth\":640,\"systemName\":\"iOS\",\"appVersion\":\"5.0.2\",\"appName\":\"paiyuanxian\",\"screenHeight\":1136,\"deviceId\":\"08b88b9f8a9375ea786b80dbe1374e62\",\"systemVersion\":\"7.1\"}" forHTTPHeaderField:@"User-Agent"];
[request addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString *sBody = @"";
for (NSString *key in dic.allKeys) {
NSString *value = [dic objectForKey:key];
if (sBody.length>0) {
sBody = [sBody stringByAppendingString:@"&"];
}
sBody = [sBody stringByAppendingFormat:@"%@=%@", key, value];
}
NSLog(@"request = %@==========",request);
[request setHTTPBody: [sBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *urlResponse;
NSData *as = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:nil];
// NSLog(@"=====%d",as.length);
NSError *error;
NSDictionary *endDic = [NSJSONSerialization JSONObjectWithData:as options:kNilOptions error:&error];
NSLog(@"%@",endDic);}
用这段代码也是返回401
用这段代码也是返回401我这边还写了一个plist文件 得读取里面的请求体啊
用这段代码也是返回401我这边还写了一个plist文件 得读取里面的请求体啊
-(void)postByAFNetworking{
NSString *str = @"http://paiyuanxian.iushare.com:8080/paiyuanxian/mainQuery.do";
NSString *urlstr = [[NSString stringWithFormat:@"%@",str]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSBundle*bundle=[[NSBundle mainBundle]init];
NSDictionary*dic=[NSDictionary dictionaryWithDictionary:[[[NSDictionary alloc]initWithContentsOfFile:[bundle pathForResource:@"urlArgument" ofType:@"plist"]]objectForKey:@"allFilmNews"]];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", nil];
[manager setRequestSerializer:[CustomSerializer serializer]];
[manager POST:urlstr
parameters:dic
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
}
@interface CustomSerializer : AFHTTPRequestSerializer@end@implementation CustomSerializer-(NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters error:(NSError *__autoreleasing *)error{
NSParameterAssert(method);
NSParameterAssert(URLString);
NSURL *url = [NSURL URLWithString:URLString];
NSParameterAssert(url);
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
mutableRequest.HTTPMethod = method;
mutableRequest.allowsCellularAccess = self.allowsCellularAccess;
mutableRequest.cachePolicy = self.cachePolicy;
mutableRequest.HTTPShouldHandleCookies = self.HTTPShouldHandleCookies;
mutableRequest.HTTPShouldUsePipelining = self.HTTPShouldUsePipelining;
mutableRequest.networkServiceType = self.networkServiceType;
mutableRequest.timeoutInterval = self.timeoutInterval;
[mutableRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[mutableRequest addValue:@"{\"device\":\"Unknown iPhone\",\"language\":\"zh-Hans\",\"screenWidth\":640,\"systemName\":\"iOS\",\"appVersion\":\"5.0.2\",\"appName\":\"paiyuanxian\",\"screenHeight\":1136,\"deviceId\":\"08b88b9f8a9375ea786b80dbe1374e62\",\"systemVersion\":\"7.1\"}" forHTTPHeaderField:@"User-Agent"];
mutableRequest = [[self requestBySerializingRequest:mutableRequest withParameters:parameters error:error] mutableCopy];
return mutableRequest;
}@end
@class AFHTTPClient;
@interface HttpClient : NSObject{
AFHTTPClient *httpClient;
}
+(HttpClient *)sharedHttp;
-(void)get:(NSString *)path block:(void (^)(id json))processJson;
-(void)post:(NSString *)path parameters:(NSDictionary *)parameters block:(void (^)(id json))processJson;
@endHttpClient.m 实现文件 #import "HttpClient.h"
#import "AFNetworking.h"
#import "AFHTTPClient.h"@implementation HttpClient
+(HttpClient *)sharedHttp
{
static HttpClient *instnace;
static dispatch_once_t run_once;
dispatch_once(&run_once,^{
instnace =[[HttpClient alloc] init];
});
return instnace;
}-(void)request:(NSString *)path method:(NSString *)method parameter:(NSDictionary *)parameter block:(void (^)(id json))processJson{ NSURL *url = [NSURL URLWithString:yourServerUrl];
httpClient =[[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request=[httpClient requestWithMethod:method path:path parameters:parameter];
AFHTTPRequestOperation *operation=[[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error = nil;
id jsonResult =[NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:&error];
if (jsonResult) {
processJson(jsonResult); ///////call back method
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Access server error:%@,because %@",error,operation.request); }];
NSOperationQueue *queue=[[NSOperationQueue alloc] init];
[queue addOperation:operation];
}-(void)get:(NSString *)path block:(void (^)(id json))processJson {
[self request:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] method:@"GET" parameter:nil block:processJson];
}
-(void)post:(NSString *)path parameters:(NSDictionary *)parameters block:(void (^)(id json))processJson {
[self request:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] method:@"POST" parameter:parameters block:processJson];
}
@end