Checking HEAD from NSMutableURLRequest without Error - ios

I'm working on a program for the iPhone and I ran into a bit of a snafu. I want to load a URL into a WebView, however, I want to make sure that the URL actually exists and is working before trying to load it into the view.
Imagine you have 200 servers and you know your file exists you just don't know which server. It is at http://serverXXX.mydomain.com/myfile.html where XXX is the server # 0 through 200
I wrote a bash script that uses cURL to loop through the servers checking the HEAD request of each iteration of the url and timing out after 1 second:
http://server1.mydomain.com/myfile.html
http://server2.mydomain.com/myfile.html
...
http://server199.mydomain.com/myfile.html
http://server200.mydomain.com/myfile.html
When it gets back a response it greps the response and compares it. If the response is OK it loads the URL in an external program and exits the script. How do I do this in Objective-C without the error "The requested URL was not found on this server." popping up?? I don't want to click OK 200 times while it tries to find the correct URL.

You can do the same as your script with an instance of NSMutableURLRequest and a NSURLConnection. You can set the request to HEAD (setHTTPMethod:) and iterate over the server URLs updating the request after each response (connection:didReceiveResponse:) is received.

Related

Quectel M95 HTTP POST timeout error

I'm trying to send a POST to a server but I always get the +CME ERROR: 3821. I know that this means "HTTP to read timeout". Then, I tried to change the server to another one, just to test, and then I get the same error 3821. My AT commands list is:
AT+CGATT=1
AT+QIFGCNT=0
AT+QICSGP=1,"zap.vivo.com.br"
AT+QIACT
AT+QILOCIP (IP OK!)
AT+QHTTPURL=38,30
CONNECT
http://www.posttestserver.com/post.php
OK
AT+QHTTPPOST=10,50,80
CONNECT
helloworld
OK
+CME ERROR: 3821
Does anyone know what is wrong?
I solved it by using directly
AT+QHTTPPOST=10,50
and not
AT+QHTTPPOST=10,50,10
hello even the issue is one year old i am writing answer if any one needs. In the source file of "ril_http.c" of Quectel modules, add delay of minimum 10ms in HTTP call back handler. it will solve the timeout error and will able to post it successfully.

Debugging an API request

I'm trying to post a user status update to the Goodreads API.
Most of the time my request returns 200 OK and does nothing. Every now and then, though, it returns 201 Created and the status is updated. When it works it's always the first time I try to make the call after running the app in iOS simulator. Subsequent calls never work.
I don't think the problem is the API itself, since the official Goodreads iOS app uses the same call and it always works.
Their API is famous for having problems with calls that include brackets in the parameters, but I can make other calls that contain brackets and they work fine, the problem is just this one.
I'm using OAuthSwift and this is my code:
oAuth.client.post(
"http://www.goodreads.com/user_status",//.xml",//?user_status[book_id]=6366035&user_status[page]=168",
parameters: ["user_status[page]" : 168, "user_status[book_id]" : 6366035, "format" : "xml"],
//headers: ["Content-Type" : "application/x-www-form-urlencoded"],
success: {
data, response in
print("")
print(response)
},
failure: {
error in
print("")
print(error)
}
)
(The commented out parts are alternatives I have tried unsuccessfully.)
I'm printing the base string that gets signed and it looks the same for the calls that work and the ones that don't, except for the nonce and the timestamp, obviously.
In the headers is also included the oauth_signature, which changes every time and sometimes contains characters that are encoded by OAuthSwift, so that could account for the call working just some of the time (it could work only when the signature doesn't contain a certain character)… but I'm printing out the headers too and I don't see any patterns or any discernible difference between the headers of the calls that work and those of the calls that don't.
So now I don't know what to test anymore… I'm checking the base string and the headers for calls that work and for calls that don't and they look the same… Could anybody think of something else that changes between calls and I should check? I have no idea what could be causing this and I don't know how to debug it.
Thanks in advance,
Daniel
Edit: Very weird… I tried my request with Paw, a Mac REST client, and with Chrome's Postman extension. If I use https I get 404 on my first call, then 201 on the second, then 404 on the third, 201 on the forth and so on. It works every other time. The time it works it doesn't matter if I use http or https, it works as long as there was a failed https request just before.
So I tried doing the same in my app: I added two https calls one after the other… in my app they always return 404.
So it seems like Postman, Paw and OAuthSwift are handling the requests differently. I don't know what could be the difference between those clients… the signature base string seems to be the same for all three, the headers too… so what else could change between them?
In the newer versions of Xcode you can only communicate with a HTTPS server. I expect Google support that so you can change the URL. Or you can edit your Info.plist file.
App Transport Security Settings > Allow Arbitrary Loads > YES

Issue with the rails (2.3.11) server(production side)

I am stuck with a very unusual issue. I am trying to call a webservice request from the server (actully an ajax 2 step request from the client side) for which i am getting an xml formatted response which i am parsing using a treeview. The issue is..its working well in development mode on local server but showing error in production with the same codes. When I initiate a request I get a response for the first step in string format but showing error "error loading xmlDoc: xmlDoc is undefined" for the second request which suppose to be initiated directly after the first response.
codes are available here:
https://github.com/domthu/microaquadt/blob/master/app/views/oligo_sequences/edit.html.erb
error is somewhere on the function starting from line 195.
Any help or suggestion would be much appreciated...!!!!

Google docs API: can't download a file, downloading documents works

I'm trying out http requests to download a pdf file from google docs using google document list API and OAuth 1.0. I'm not using any external api for oauth or google docs.
Following the documentation, I obtained download URL for the pdf which works fine when placed in a browser.
According to documentation I should send a request that looks like this:
GET https://doc-04-20-docs.googleusercontent.com/docs/secure/m7an0emtau/WJm12345/YzI2Y2ExYWVm?h=16655626&e=download&gd=true
However, the download URL has something funny going on with the paremeters, it looks like this:
https://doc-00-00-docs.googleusercontent.com/docs/securesc/5ud8e...tMzQ?h=15287211447292764666&amp\;e=download&amp\;gd=true
(in the url '&amp\;' is actually without '\' but I put it here in the post to avoid escaping it as '&').
So what is the case here; do I have 3 parameters h,e,gd or do I have one parameter h with value 15287211447292764666&ae=download&gd=true, or maybe I have the following 3 param-value pairs: h = 15287211447292764666, amp;e = download, amp;gd = true (which I think is the case and it seems like a bug)?
In order to form a proper http request I need to know exectly what are the parameters names and values, however the download URL I have is confusing. Moreover, if the params names are h,amp;e and amp;gd, is the request containing those params valid for obtaining file content (if not it seems like a bug).
I didn't have problems downloading and uploading documents (msword docs) and my scope for downloading a file is correct.
I experimented with different requests a lot. When I treat the 3 parameters (h,e,gd) separetaly I get Unauthorized 401. If I assume that I have only one parameter - h with value 15287211447292764666&ae=download&gd=true I get 500 Internal Server Error (google api states: 'An unexpected error has occurred in the API.','If the problem persists, please post in the forum.').
If I don't put any paremeters at all or I put 3 parameters -h,amp;e,amp;gd, I get 302 Found. I tried following the redirections sending more requests but I still couldn't get the actual pdf content. I also experimented in OAuth Playground and it seems it's not working as it's supposed to neither. Sending get request in OAuth with the download URL responds with 302 Found instead of responding with the PDF content.
What is going on here? How can I obtain the pdf content in a response? Please help.
I have experimented same issue with oAuth2 (error 401).
Solved by inserting the oAuth2 token in request header and not in URL.
I have replaced &access_token=<token> in the URL by setRequestHeader("Authorization", "Bearer <token>" )

405 Method not allowed on Net::HTTP request [ruby on rails]

I'm trying to verify if there is a remote url with following code:
endpoint_uri = URI.parse(#endpoint.url)
endpoint_http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
endpoint_request = Net::HTTP::Head.new(endpoint_uri.request_uri)
endpoint_response = endpoint_http.request(endpoint_request)
I'm still getting 405 Method not allowed. When I use Get instead Head in Net::HTTP::Head.new I'm getting 200 Success but also with whole remote document in response what results in bigger response time (0.3s => 0.9s).
Any ideas why this is happening? Thx
There's a chance that the #endpoint url you're trying to interact with doesn't support HEAD requests (which would be really weird, but still may be the case). Your code works fine for me with a handful of urls (google.com, stackoverflow.com, etc.)
Have you tried a curl request to see what it returns?
curl -I http://www.the_website_you_want_to_test.com

Resources