Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6247237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:50:00+00:00 2026-05-24T12:50:00+00:00

I override NSURLProtocol and need to return HTTP response with specific statusCode. NSHTTPURLResponse does

  • 0

I override NSURLProtocol and need to return HTTP response with specific statusCode. NSHTTPURLResponse does not have statusCode setter, so I tried to override it with:

@interface MyHTTPURLResponse : NSHTTPURLResponse {} 

@implementation MyHTTPURLResponse

    - (NSInteger)statusCode {
        return 200; //stub code
    }
@end

Overridden startLoading method of NSURLProtocol looks like this:

-(void)startLoading
{   
   NSString *url = [[[self request] URL] absoluteString];
   if([url isEqualToString:SPECIFIC_URL]){
       MyURLResponse *response = [[MyURLResponse alloc] initWithURL:[NSURL URLWithString:@"http://fakeUrl"]
       MIMEType:@"text/plain"
       expectedContentLength:0  textEncodingName:nil];

       [[self client] URLProtocol:self     
            didReceiveResponse:response 
            cacheStoragePolicy:NSURLCacheStorageNotAllowed];

       [[self client] URLProtocol:self didLoadData:[@"Fake response string"
            dataUsingEncoding:NSASCIIStringEncoding]];

       [[self client] URLProtocolDidFinishLoading:self];                

       [response release];

    }
    else{   
        [NSURLConnection connectionWithRequest:[self request] delegate:self];   
    }
}

But this approach does not work, the response created in NSURLProtocol is always with statusCode = 0 on web page. At the same time responses that are returned from network by NSURLConnection have normal expected statusCodes.

Can anyone please help with ideas how to set statusCode explicitly for created NSURLResponse? Thanx.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:50:03+00:00Added an answer on May 24, 2026 at 12:50 pm

    I’ve implemented custom init method using following code:

        NSInteger statusCode = 200;
        id headerFields = nil;
        double requestTime = 1;
    
        SEL selector = NSSelectorFromString(@"initWithURL:statusCode:headerFields:requestTime:");
        NSMethodSignature *signature = [self methodSignatureForSelector:selector];
    
        NSInvocation *inv = [NSInvocation invocationWithMethodSignature:signature];
        [inv setTarget:self];
        [inv setSelector:selector];
        [inv setArgument:&URL atIndex:2];
        [inv setArgument:&statusCode atIndex:3];
        [inv setArgument:&headerFields atIndex:4];
        [inv setArgument:&requestTime atIndex:5];
    
        [inv invoke];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to override property setter of the parent class. In order to temporary block
I need to override the 'user/%user/edit' page for a specific user role, say 'foo_role'.
protected override Boolean IsValid(String propertyValue) { return !String.IsNullOrEmpty(propertyValue) && propertyValue.Trim().Length > 0; } This
I override ProcessCmdKey() in my MDI parent form class and have some keyboard shortcut
I tried to override the settings in the default stlyesheet that comes with the
Can I override an ActionResult method. Say I have a method Index in AccountController
I need to override the behavior of the find method of a class from
I've tried to override error message when input incorrect data type in input field
Why is new/override required on abstract methods but not on virtual methods? Sample 1:
@Override protected Dialog onCreateDialog(int id) { switch (id) { case IDD_COLOR: return new AlertDialog(this);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.