1

我在 gcdwebserver 上工作,我正在向 gcdwebserver 发送 post 请求,也发送一些参数,但是在 addhandlermatchblock 中没有得到查询参数,请回复

代码:

    NSString * postString=@"name=ram&age=20";
   NSURLSession * session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURL * url=[NSURL URLWithString:@"http://127.0.0.1:8080/index.html"];
    NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 


[webServer addHandlerWithMatchBlock:^GCDWebServerRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) {

        if (![requestMethod isEqualToString:@"GET"]) {
            return nil;
        }


        return [[GCDWebServerRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];

    } processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
            NSLog(@"coming here get %@",request.urlQuery); // null
        GCDWebServerResponse* response = nil;

        return response;

                              }];
4

1 回答 1

0

我尝试用 GCDWebServerURLEncodedFormRequest 代替 GCDWebServerRequest ,它起作用了

  [webServer addHandlerWithMatchBlock:^GCDWebServerURLEncodedFormRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) {

            if (![requestMethod isEqualToString:@"GET"])
            {
                return [[GCDWebServerURLEncodedFormRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
            }
            else
            {
                return [[GCDWebServerURLEncodedFormRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
            }

        } processBlock:^GCDWebServerResponse *(GCDWebServerURLEncodedFormRequest * request) {

 NSMutableDictionary * dic=[[NSMutableDictionary alloc]initWithDictionary:[request arguments]]; // url query params


    return response;
    }
于 2016-05-23T05:14:55.437 回答