Bad URL when requesting with NSURL - iphone

I'm trying to request this kind of URL in iPhone 4.0 SDK (access token slightly modified because you don't really need to see it):
https://graph.facebook.com/me?sdk=ios&sdk_version=2&access_token=123902817987|8cb9e8408d2685cef853cd80.9-747882379|UGu5NvcAHiXuGEGzkq&format=json&limit=40&until=1286619821
But I got this message:
Failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x9e657a0 {NSUnderlyingError=0x9e656a0 "bad URL", NSLocalizedDescription=bad URL}
When I copied and pasted in Safari or Chrome, it works. I tried replacing | with %| as suggested here but doesn't work.
Asking it from the terminal looks like this:
curl https://graph.facebook.com/me/statuses?sdk=ios&sdk_version=2&access_token=9999955817987|8ab9e8408d2685cef.3-747882379|UGuxWDuM&format=json&limit=40&until=1286619821
[1] 16190
[2] 16191
[3] 16194
[4] 16195
[5] 16196
[2] Done sdk_version=2
[4]- Done format=json
$ -bash: UGu5NvcAHiXuGEGzkq3KP8xWDuM: command not found
-bash: 8ab9e8408d2685cef853cd80.3-747882379: command not found
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}
[1] Done curl https://graph.facebook.com/me/statuses?sdk=ios
[3]- Exit 127 access_token=9999955817987 | 8ab9e8408d2685cef.3-747882379 | UGuxWDuM
[5]+ Done limit=40
Any ideas?

NSString *strURL = [loc_address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// requesting weather for this location ...
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat: #"http://www.google.com/ig/api?weather=%#", strURL]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
[req setHTTPMethod:#"POST"];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];

You need to replace the pipe character in the URL with %7C. This is the URL encoded value.
Also, the URL needs to be enclosed in quotation marks in order for it to work with curl.

ho ho ho.
Set location_id using the id of the venue you want.
This make me waste a lot of time.
One of dozen facebook secrets is debug network when making what you want, in this case creating a event. You'll see the correct params names for what you need.

This error message, also can be caused by any error in the URL typing. Usually some white space in the URL string.

Related

connecting to WCF from iOS

Ugh. I have read a few similar questions here on SOF but so far none of those solutions have worked for me.
I am trying to connect an iOS7 client to a WCF web service. Let me go ahead and point out that I didn't write the WCF service and have never written one - so I'm pretty WCF stupid. As I understand it - the idea is basically create a SOAP xml packet, and send it over, and wait for an xml response. There is a test client running on .NET that works fine, so I had the guy who wrote that turn on Fiddler so I could see what the xml and request headers should look like. Mine now are identical to his, yet the only response I can muster from the server is:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:ActionNotSupported</faultcode><faultstring xml:lang="en-US">The message with Action 'http://www.tempuri.org/IFoolSpoon_SoWo_Service/Check_Item_test' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).</faultstring></s:Fault></s:Body></s:Envelope>
Since that is scrolling WAY off the screen - it's a ContractMismatch error - whatever that actually means.
Side challenge: in googling the above error message - it seems impossible for anyone to explain the term "Contract" without using the word "contract". ;) Every explanation I saw was basically "it's like...the contract - you know...like the contract between the client and server...it's like a contract." :(
Anyway, here is the relevant obj-c code, with names/urls changed to protect the guilty:
// construct a URL that will ask the service for what we want
NSURL *url = [NSURL URLWithString:#"http://mymachine.myisp.com/SOWO/MyService.svc"];
// build the SOAP envelope
NSString *soapMessage = [NSString stringWithFormat:
#"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<s:Body >\n"
"<Check_Item_test xmlns=\"http://derp.org/\">\n"
"<UPC>1090000021</UPC>\n"
"</Check_Item_test>\n"
"</s:Body>\n"
"</s:Envelope>\n"];
soapMessage = [soapMessage stringByReplacingOccurrencesOfString:#"\n" withString:#"\r\n"];
// put URL into a NSURLRequest
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
NSLog(#"length = %#", msgLength);
[req addValue: #"text/xml" forHTTPHeaderField:#"Content-type"];
[req addValue: #"http://www.tempuri.org/IServiceInterface/Check_Item_test"
forHTTPHeaderField:#"SOAPAction"];
[req addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[req addValue:#"100-continue" forHTTPHeaderField:#"Expect"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
Some notes about the above code. There is another developer calling the same service from Ruby and he mentioned a couple of things: changing the line endings to \r\n from \n was his suggestion, since he found out that was a problem for him. I also adjusted some header capitalizations: SOAPaction to SOAPAction, etc. So now I'm matching HIS xml/headers exactly too, but no luck.
My request looks like this (from an OSX Fiddler equivalent):
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body >
<Check_Item_test xmlns="http://derp.org/">
<UPC>1090000021</UPC>
</Check_Item_test>
</s:Body>
</s:Envelope>
That seems to match every other client and all of them work save mine. So yeah - help? I am one degree away from the web.config for the service and I have access to the .svclog on the server. Interestingly enough - my requests aren't getting logged, even tho logMalformedRequests is set to true. Suggestions on where else to check for whatever bitbucket my requests are going into are appreciated.
So yeah - annoyed, frustrated, tired. For something that should be so simple, this has turned out to be a gigantic pain. It's probably something stupid, so perhaps I just need more eyes on it than my own.
TIA.
So the answer turned out to be: stop using SOAP.
I ended up rewriting the service endpoints as REST endpoints and all is fine and dandy.

bad URL Connection Error

I am at my wits end with this one. I am attempting to make a connection to a website database. The URL I am using is: "https://subdomain.domain.com/scripts/script.php?v=YES&userid=xxxxxxxxxx&aid=yyyy&msg=test message"
Prior to sending the URL I encode the string as follows:
NSString *strURL = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
url = [url stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
theURL = [NSURL URLWithString:strURL];
When I attempt to connect to the server I get the following error message:
NSLocalizedDescription = "bad URL";
NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1000 \"bad URL\" UserInfo=0xa11d760 {NSLocalizedDescription=bad URL
I cannot for the life of me figure out what is going on. Here are some of the actions I have taken to troubleshoot:
I have tested other URLs in the app encoded the same way and have no problem.
I entered the URL directly in Safari, Google and Mozilla with success.
When I tested the URL with curl in the Terminal I discovered the key-value pairs in the query where not being assigned, for example userid was null.
When I pasted escaped text into the curl command line the result was successful.
I do not know what to do here. Any thoughts are greatly appreciated!
I figured out the problem -- seems the URL set to the connection was not escaped properly. I passed the wrong string value to the method. As you can see in the code I passed URL instead of strURL.
Thanks for your help!

GET http requests In Objective C

I have the following code in objective c that is supposed to make a GET http request to my website which in turn will submit something into my MySQL database...
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.website.com/VideoPush/plist/urlTransfer.php"]];
[request setHTTPMethod:#"GET"];
NSString *post =[[NSString alloc] initWithFormat:#"videoID=%#",videoURL];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
Note: I'm a total beginner to http requests with Objective c so I realize I could be missing something obvious...
Now if I run the following url in my browser...
http://www.website.com/VideoPush/plist/urlTransfer.php?videoID=hhklskdjsad
Then something gets entered into the database, but not when I'm using objective c. Also what is the difference between GET and POST when you make requests like this (since the user doesn't see the url anyways)?
ALSO: Am I allowed to pass a url (still a nsstring with /'s though) as the videoURL variable above
You've tried to set the body of a GET request, which makes no sense. What you probably want is:
NSString *urlString = [NSString stringWithFormat:#"http://www.website.com/VideoPush/plist/urlTransfer.php?videoID=%#", videoURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
... the rest of your method ...
You should probably go and have a read up about what a GET and a POST is and why I said it makes no sense to have a body in a GET request.
ALSO: Am I allowed to pass a url (still a nsstring with /'s though) as the videoURL variable above
No, you will need to URL encode anything in the query string. You can use CFURLCreateStringByAddingPercentEscapes to do this for you.

Empty body in POST in ASIHTTPRequest

Basically, I am sending a POST request with an empty data body:
ASIHTTPRequest *request [ASIHTTPRequest alloc] init];
[request setURL:[NSURL URLWithString:escapedUrlString]];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Content-Type" value:#"application/xml"];
[request startSynchronous];
But i am getting this response each time:
Incorrect NSStringEncoding value 0x0000 detected. Assuming NSStringEncodingASCII. Will stop this compatiblity mapping behavior in the near future.
I am wondering if it's mandatory to set post values.
I don't have much experience with the ASIHTTPRequest wrapper but I can see you are initialising it with alloc:init whilst most examples I've seen have a convenience initialiser requestWithURL:(NSURL*)url i.e.
ASIHTTPRequest *request = [AIHTTPRequest requestWithURL:escapedUrlString];
At a guess I'd say this convenience initaliser will also set some of the required variables for your post request to work, including NSStringEnconding.
From the ASIHTTPRequest documentation at http://allseeing-i.com/ASIHTTPRequest/How-to-use#handling_text_encodings
Handling text encodings
ASIHTTPRequest will attempt to read
the text encoding of the received data
from the Content-Type header. If it
finds a text encoding, it will set
responseEncoding to the appropriate
NSStringEncoding. If it does not find
a text encoding in the header, it will
use the value of
defaultResponseEncoding (this defaults
to NSISOLatin1StringEncoding).
When you call [request
responseString], ASIHTTPRequest will
attempt to create a string from the
data it received, using
responseEncoding as the source
encoding.
As Rog says, use initWithURL or requestWithURL. Anyway, in my case the problem was that I was connecting to a slow server and the request timed out giving the "Incorrect NSStringEncoding value 0x0000 detected" error.
I solved it with:
request.timeOutSeconds = 50;
You can try with more than 50 second if your server is even slower.

I need the correct syntax for the setValue:forHTTPHeaderField: method on NSMutableURLRequest

I am pulling my hair out trying to conjure up the correct syntax to set the HTTP header information do a byte-range load from an HTTP server.
This is the offending method on NSMutableURLRequest
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field
This is how I am using this method to load the first 512 byte of a URL request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"0-512\r\n" forHTTPHeaderField:#"Range"];
So far it is ignored and I always receive the entire data payload. I just want the range of bytes specified (0 - 512). Can someone please relieve my headache?
Update:
I have used curl to confirm that my web server supports byte ranges thusly:
curl --range 0-2047 http://www.somewhere.com/humungodata.dat -o "foobar"
The file size of foobar is 2048
Cheers,
Doug
Problem solved.
By adding additional header fields the code immediately worked correctly. Why? Dunno. But it works:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"keep-live" forHTTPHeaderField:#"Connection"];
[request setValue:#"300" forHTTPHeaderField:#"Keep-Alive"];
[request setValue:#"bytes=0-2047" forHTTPHeaderField:#"Range"];
Your original code was wrong in the setValue line, it should be #"bytes=0-512". In your followup, you used the correct string, so the other headers should not be needed.
What you have there should be the correct way to add a header value to a URL request, however i thought only posts got header values, maybe im wrong, have you tried doing this on other enviroments and gotten it to work? Maybe take out the \r\n?