如何将POST值发送到网站?

时间:2012-09-10 03:39:24

标签: iphone objective-c asihttprequest

我需要通过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 以下是请求和响应标头:Header

我收到200回复​​,但我发送的价值没有显示在网站上。有人可以建议我吗?

由于

2 个答案:

答案 0 :(得分:0)

我注意到您要发布到HTML文件。除非您有一些特殊设置允许HTML文件可执行,否则html文件将不会处理您发布给它的数据。是仅显示一个值还是未显示所有值。如果缺少所有值,那么我最初说的是正确的,你需要使用像PHP,CF,Perl或任何你希望从你的应用程序发布的数据的语言。

答案 1 :(得分: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);
}