gpt4 book ai didi

ios - 在 ios 中使用查询字符串创建 NSURLRequest

转载 作者:可可西里 更新时间:2023-11-01 04:14:03 26 4
gpt4 key购买 nike

我在 ios 中创建了一个用户查询表单。我正在使用 php 作为服务器端。我在 IOS 中构建了如下所示的查询字符串

http://www.mydomain.in/androidmail_enquiry.php?name=Vinoth Kumar&phone=04259280244&email=vinoth@ldomain.com&address=Coimbatore&comments=Sample Enquiry&mobile=xxxxxxxx

代码:

-(void)sendEnquiryDetails
{
cmttextview.text = @"Sample Enquiry";
NSString *siteurl = @"http://www.mydomain.in/androidmail_enquiry.php?";
NSString *name = txtName.text;
NSString *phone = txtPhone.text;
NSString *email = txtEmail.text;
NSString *address = txtAddress.text;
NSString *comments = cmttextview.text;
NSString *mobile = txtMobile.text;
NSString *enquiryurl = [NSString stringWithFormat:@"%@name=%@&phone=%@&email=%@&address=%@&comments=%@&mobile=%@",siteurl,name,phone,email,address,comments,mobile];

NSLog(enquiryurl);

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:enquiryurl]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//NSLog(@"didReceiveResponse");
[self.responseData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@",[NSString stringWithFormat :@"didfailwitherror: %@", [error description]]);
}

-(void)connectionDidFinishLoading: (NSURLConnection *)connection{
NSLog(@"Success Code:%@",self.responseData);
}

但是当我使用 NSURLRequest 提交这个时,在 didFailWithError 方法中出现了无效 url(URL 形成错误)之类的错误。

最佳答案

确保您已添加 stringByAddingPercentEscapesUsingEncoding ,因为在您的 url 中名称中有空格,因此在浏览器中它被替换为 %20 您必须在代码中以编程方式添加,

试试这个

-(void)sendEnquiryDetails
{
cmttextview.text = @"Sample Enquiry";
NSString *siteurl = @"http://www.mydomain.in/androidmail_enquiry.php?";
NSString *name = txtName.text;
NSString *phone = txtPhone.text;
NSString *email = txtEmail.text;
NSString *address = txtAddress.text;
NSString *comments = cmttextview.text;
NSString *mobile = txtMobile.text;
NSString *enquiryurl = [NSString stringWithFormat:@"%@name=%@&phone=%@&email=%@&address=%@&comments=%@&mobile=%@",siteurl,name,phone,email,address,comments,mobile];

NSLog(enquiryurl);

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[enquiryurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

关于ios - 在 ios 中使用查询字符串创建 NSURLRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19263186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com