菜鸟教程小白 发表于 2022-12-13 00:24:40

ios - iPhone上的错误URL,当我在浏览器中输入它时它可以工作


                                            <p><p>我正在制作一个 jsonstring。当我执行它时,当我在浏览器中执行它时它就可以工作。我通过记录确切的 url 并将其复制到浏览器中来做到这一点。比我得到我想要的 HTTP Get,但在 iPhone 中我只得到一个错误的登录。</p>

<pre><code>- (IBAction)getDown:(id)sender { //perform get request
    NSLog(@&#34;beginnen met versturen&#34;);

    //NSString * _barCode = [ objectForKey:@&#34;phoneNumber&#34;];

    //build up the request that is to be sent to the server
    //NSString*jsonString = [ initWithFormat:@&#34;{\&#34;barcode\&#34;:\&#34;%@\&#34;}&#34;, _barCode];


    NSString*jsonString = [ initWithFormat:@&#34;{\&#34;barcode\&#34;:\&#34;123456\&#34;}&#34;];

    NSString *str = ;
    NSLog(@&#34;%@&#34;, str);
    NSMutableURLRequest *request = [ initWithURL:];
    NSLog(@&#34;url: %@&#34;, request);

    ;
   // ; //selects what task the server will perform
    NSLog(@&#34;met value: %@&#34;, request);

    //initialize an NSURLConnectionwith the request
    NSURLConnection *connection = [ initWithRequest:request delegate:self];
    if(!connection){
      NSLog(@&#34;Connection Failed&#34;);
    }

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ // executed when the connection receives data

    receivedData = data;
    /* NOTE: if you are working with large data , it may be better to set recievedData as NSMutableData
   and use here, in this event you should also set recievedData to nil
   when you are done working with it or any new data received could end up just appending to the
   last message received*/
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ //executed when the connection fails

    NSLog(@&#34;Connection failed with error: %@&#34;,error.localizedDescription);
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{

    /*This message is sent when there is an authentication challenge ,our server does not have this requirement so we do not need to handle that here*/
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSLog(@&#34;Request Complete,recieved %d bytes of data&#34;,receivedData.length);

    //;//send the data to the delegate
    NSData *data = receivedData;
    NSDictionary *dictionary = ;
    NSLog(@&#34;%@&#34;,dictionary.JSONString ); ; // set the textview to the raw string value of the data recieved

    NSString *value1 = ;
    NSLog(@&#34;%@&#34;, value1);
    NSString *value2 = ;
    NSLog(@&#34;%@&#34;,dictionary);
    NSLog(@&#34;%@&#34;, value2);

}
</code></pre>

<p>这是日志:</p>

<pre><code>2013-01-10 16:31:46.550 Scanner http://server.nl/scan.php?data={&#34;barcode&#34;:&#34;123456&#34;}
2013-01-10 16:31:46.551 Scanner url: &lt;NSMutableURLRequest (null)&gt;
2013-01-10 16:31:46.553 Scanner met value: &lt;NSMutableURLRequest (null)&gt;
**2013-01-10 16:31:46.556 Scanner Connection failed with error: bad URL**
</code></pre>

<p>当我从字符串中删除完整的 json 时,我没有得到错误的 url。所以可能有问题。有谁知道我做错了什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在执行 URL 请求之前,您需要对其进行编码。
最好和最优雅的解决方案是在 NSString 上添加一个类别,例如:</p>

<pre><code>- (NSString*)URLEncode {
    // Should not be encoded:-_.
    return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(&#34;;/?:@&amp;=+$,!*&#39;()&lt;&gt;#%\&#34;{}|\\^[]`~&#34;), kCFStringEncodingUTF8) autorelease];
//
}
</code></pre>

<p>当你提出请求时:</p>

<pre><code>NSString *str = ;
NSMutableURLRequest *request = [ initWithURL:];
</code></pre>

<p>如果您不想使用其他文件(甚至认为会推荐),请将此方法添加到您的类中:</p>

<pre><code>- (NSString*)URLEncode:(NSString )yourURL {
// Should not be encoded:-_.
return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(&#34;;/?:@&amp;=+$,!&#39;()&lt;&gt;#%\&#34;{}|\\^[]`~&#34;), kCFStringEncodingUTF8) autorelease];
}
</code></pre>

<p>并使用</p>

<pre><code>NSMutableURLRequest *request = [ initWithURL:];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - iPhone上的错误URL,当我在浏览器中输入它时它可以工作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/14261548/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/14261548/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - iPhone上的错误URL,当我在浏览器中输入它时它可以工作