解决方案 »

  1.   

    你们公司自己的app么?直接问服务器那边好了
      

  2.   

    不是   正在学习IOS中   这个是用抓包软件抓的  接口.  想练习写一个这样的软件   但是数据请求不下来.  用系统带的能请求下来 但是想用一下   AFNetWorking  
      

  3.   

    把你用系统自带测试成功的代码贴上来-(void)downLoad
    {
        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);}
      

  4.   

    把你用系统自带测试成功的代码贴上来-(void)downLoad
    {
        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
      

  5.   

    把你用系统自带测试成功的代码贴上来

    用这段代码也是返回401我这边还写了一个plist文件  得读取里面的请求体啊
      

  6.   

    把你用系统自带测试成功的代码贴上来

    用这段代码也是返回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
      

  7.   

    如果使用AFNetwork 网络请求组件的话,更容易实现,你可以简单做一下封装HttpClient.h 头文件 #import <Foundation/Foundation.h>
    @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