0

我需要通过 UITextField 从我的应用程序提交值,我希望这个值显示在我发送请求的网站上。我使用 ASIHTTPRequest 向网站发送请求。我试过这样的事情:

    NSURL *url = [NSURL URLWithString:@"http://www.project4hire.com/freelance_job_16265.html"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
//
[request setPostValue:priceField  forKey:@"bid"];
[request setPostValue:dayField forKey:@"days2c"];
[request setPostValue:commentField forKey:@"comment"];
[request setPostValue:@"1" forKey:@"notify"];
[request setPostValue:@"placebid" forKey:@"Place Bid >>"];
[request setPostValue:@"e6fb12104854e6e9" forKey:@"suid"];
[request setPostValue:@"placebid" forKey:@"a"];
[request setPostValue:@"16265" forKey:@"pid"];

[request setDelegate:self];
[request setDidFailSelector:@selector(requestBidFailed:)];
[request setDidFinishSelector:@selector(requestBidFinished:)];
[request startAsynchronous];

}
- (void)requestBidFailed:(ASIHTTPRequest *)request
{
//notify user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error sending         request to the server" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

}

- (void)requestBidFinished:(ASIHTTPRequest *)request
{
NSLog(@"Status: %d", request.responseStatusCode);
NSLog(@"string: %@",request.responseString);
}

这是投标表格:BidForm 这是请求和响应标头:标头

我收到回复 200,但我发送的值未显示在网站上。谁能给我建议?

谢谢

4

2 回答 2

0

我有同样的问题,但我的在模拟器上工作,但在设备上没有工作,然后我读了一篇文章,它显示 ASIHTTPRequest API 的创始人不再更新他们的库(我不知道那篇文章是否可靠) ,所以我决定使用一个更新的库,即 RestKit。您可以从这个网站下载并设置它:restkit.org,如果您在安装时遇到任何问题,可以请我帮助您。以下是在 restkit 库上发布的简单代码:

- (void)post
{
    [RKClient clientWithBaseURLString:@"http://www.project4hire.com"];

    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            priceField, @"bid",
                            dayField, @"days2c", nil];

    [[RKClient sharedClient] post:@"/freelance_job_16265.html" params:params delegate:self];
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    NSRange range = [[error localizedDescription] rangeOfString:@"-1012"];
    if (range.length > 0){
        //Do whatever here to handle authentication failures
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error sending         request to the server" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    RKLogError(@"Hit error: %@", error);
}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
    if ([request isGET]) {
        // Handling GET /foo.xml

        if ([response isOK]) {
            // Success! Let's take a look at the data
            NSLog(@"Retrieved XML: %@", [response bodyAsString]);
        }

    } else if ([request isPOST]) {

        // Handling POST /other.json
        if ([response isJSON]) {
            NSLog(@"Got a JSON response back from our POST!");
        }

    } else if ([request isDELETE]) {

        // Handling DELETE /missing_resource.txt
        if ([response isNotFound]) {
            NSLog(@"The resource path '%@' was not found.", [request resourcePath]);
        }
    }

    NSLog(@"HTTP status code:     %d", response.statusCode);
    NSLog(@"HTTP status message:  %@", [response localizedStatusCodeString]);
    NSLog(@"Header fields: %@", response.allHeaderFields);
    NSLog(@"Body: %@", response.bodyAsString);
}
于 2012-09-10T04:12:41.330 回答
0

我注意到您正在发布到 HTML 文件。除非您有一些特殊设置以允许 HTML 文件可执行,否则 html 文件将不会处理您发布给它的数据。是只有一个值没有显示还是所有值都没有显示。如果所有值都丢失,那么我最初所说的内容是正确的,您将需要使用 PHP、CF、Perl 或任何您想要接收从应用程序发布的数据的语言。

于 2012-09-10T03:49:28.470 回答