将 NSMutableURLRequest 与 JSP 结合使用
我正在尝试从 iPhone 应用程序发送带有一些参数的 HttpRequest。形式是这样的:
foo.jsp
<form action="/foo" method="post">
<div>
<input type="hidden" name="id" value="1" />
<input type="hidden" name="vendidas" value="25" />
</div>
<div><input type="submit" value="Send!" /></div>
</form>
所以在 iPhone 方法中,当用户按下按钮时是:
NSString *myRequestString = @"id=3&vendidas=10";
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://localhost:8888/"]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: myRequestData];
NSError *error;
NSURLResponse *response;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
我猜问题是告诉 POST 该操作是“/foo”,但不确定是不是这样。这是我第一次这样做,谷歌没有找到任何帮助。
I'm trying to send an HttpRequest from an iPhone app with some parameters. The form is like this:
foo.jsp
<form action="/foo" method="post">
<div>
<input type="hidden" name="id" value="1" />
<input type="hidden" name="vendidas" value="25" />
</div>
<div><input type="submit" value="Send!" /></div>
</form>
So in the iPhone method when user pushes a botton is:
NSString *myRequestString = @"id=3&vendidas=10";
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://localhost:8888/"]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: myRequestData];
NSError *error;
NSURLResponse *response;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
I guess the problem is to tell the POST that the action is "/foo", but not sure is that. This is my first time doing this and haven't found any help with Google.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您回答了自己的问题 - URL 不包含
foo.jsp
的路径。尝试将http://localhost:8888/
更改为http://localhost:8888/path/to/foo.jsp
。You answered your own question - the URL does not contain path to
foo.jsp
. Try to changehttp://localhost:8888/
tohttp://localhost:8888/path/to/foo.jsp
.