I have a question about URI templates in WCF Services - c#

I have a web service with following operation contract and my service is hosted at http://localhost:9002/Service.svc/
[OperationContract]
[WebGet(UriTemplate = "/Files/{Filepath}")]
Stream DownloadFile(string Filepath);
This web service would let users download file, if the proper filepath is provided (assuming, I somehow find out that proper filepath).
Now, I can access this service from a browser by typing, http://localhost:9002/Service.svc/Files/(Filepath}
If {filepath} is some simple string, its not a problem, but I want to send the location of the file. Lets us say users want to download file C:\Test.mp3 on the server. But how can I pass C:\Test.mp3 as {Filepath}? I get an error when I type http://localhost:9002/Service.svc/Files/C:\Test.mp3 in the browser.
I am new to web services and find that this community is the quickest way to get answers to my questions.
got it working now.
need to use HttpServerUtility.UrlTokenDecode() from System.web

You cannot type special characters like ":" and "\" as part of a URL. They will need to be URL encoded.

Sounds like you need to URL encode the string, then decode on the back end. Add System.Web to your project and use System.Web.HttpUtility.UrlEncode() then System.Web.HttpUtility.UrlDecode()

Related

Retrieve files from cloud public folder

We are creating c# console beta version app for our clients in which they just paste the public folder/file URL of Google drive OR one drive OR drop box OR etc. And in back-end we need to retrieve the file and process it...
I just wanted to know how do we retrieve those cloud files without any prompts for authentication(as given URL will b public, so it should not ask for Id pw)
Any help from you all experts?
With OneDrive, you can use the "shares" API to retrieve a sharing link without authentication.
You just need to encode the sharing URL correctly and then pass that to the API endpoint. The details of encoding are on the page above, but it's just URL safe base64 encoding.
GET https://api.onedrive.com/shares/{encoded_sharing_url}/root/content
The API will return the content of the file.
Edit: I got the URL slightly wrong. The /shares/ API returns a "sharing root" which looks somewhat like a drive object. To access the actual shared file, you need to add /root before the /content part of the path. I've updated this above.

Consume web service using post in c#

Currently I have less information about this, but I will update soon with more details.
I have been provided with a web page which provides user name & password option to access a url & return a token value.
The form contains that url in action & the method used is POST.
I have tried general HttpWebRequest methods to access the url, but have been unsuccessful so far. It gives error "cannot connect to remote server"
The url is like
www.somesite.com\method.php\level1\method1\xml
The url confuses me as other links have some extension at the end(I am relatively new to this web service)
I have been told this webservice has been consumed in mobile.
Can someone please guide me as to how to access a web service in c# using POST method?
I apologise for the lack of detailed information
First try to understand how the webservice works.
You can simply create a simple c# rest web service. To return json data , you will need to specify the return data format on specific operation in the web service. To use mysql for database, you can search for mysql connector for c# on google. You can find libraries to use in your code. Hope it helps !
http://www.codeproject.com/Articles/201901/CREATE-RESTful-WCF-Service-API-Using-POST-Step-By
I was able to access by providing proxy details.

Handling Authentication for a File Display using a Web Service

This is my first time developing this kind of system, so many of these concepts are very new to me. Any and all help would be appreciated. I'll try to sum up what I'm doing as efficiently as possible.
Background: I have a web application running AngularJS with Bootstrap. The app communicates with the server and DB through a web service programmed using C#. On the site, users can upload files and reference them later using direct links. There's no restriction to file type (yet), so just about anything is allowed.
My Goal: Having direct links creates a big security problem for me, since the documents/images are supposed to be private data. What I would prefer to do is validate a user's credentials when the link is clicked, then load the file in the browser using a more generic url path.
--Example--
"mysite.com/attachments/1" ---> (Image)
--instead of--
"mysite.com/data/files/importantImg.jpg"
Where I'm At: Not very far. My first thought was to add a page that sends the server request and receives a file byte stream along with mime type that I can reassemble and present to the user. However, I have no idea if this is possible using a web service that sends JSON requests, nor do I have a clue about how the reassembling process would work client-side.
Like I said, I'll take any and all advice. I'd love to learn more about this subject for future projects as well, but for now I just need to be pointed in the right direction.
Your first thought is correct, for it, you need to use the Response object, and more specifically the AddHeader and Write functions. Of course this will be a different page that will only handle file downloads, so it will be perfectly fine in your JSON web service.
I don't think you want to do this with a web service. Just use a regular IHttpHandler to perform the validation and return the data. So you would have the URL "attachments/1" get rewritten to "attachments/download.ashx?id=1". When you've verified access, write the data to the response stream. You can use the Content Disposition header to set the file name.

Unable to pass a URL in GET request to a RESTful web service in C#

I am stuck at an unexpected issue in my project. The issue is that there is a URL produced on the fly in my code that I have to submit it to a RESTful web service via a GET request. For e.g. the URL to submit looks like this: http://mysampleserver.com:8080/calc/8999/bpaX
The RESTful server accepts URL as its last parameter in the format below:
http://myRestfulAPI.domainname.com/capture/bbbb/http://mysampleserver.com:8080/calc/8999/bpaX
I also used System.Net.HttpUtility.UrlEncode(....) to encode the "URL to submit" first to incorporate it in the RESTful service call.
That resulted in getting the error below:
System.Web.HttpException: A potentially dangerous Request.Path value was detected from the client (:)
To try to resolve it, I followed the steps described per this web page but no luck.
I am using MVC 4 to implement the RESTful API in C#.
Any clue or idea how to get around this showstopper issue?
There are at least two solutions I can think of.
Change your RESTFul service to use post, because you send information to your server, and potentially it will change your resource status, based on HTTP protocol , you should use POST anyway.
You can also encode your url with Base64
The steps that you've tried are the correct steps. See also this question potentially dangerous... which is the same issue.
There are a number of characters that .NET doesn't allow in in a URL by default, and the : is one of them (as a query string, at least). They are 'potentially dangerous'. Making this change to the configuration file allows these characters to be passed through to your application.
You need to Url.Encode the url in the query string (mvc parameters) otherwise it is interpreted as more URL encoding for MVC to decode as parameters. Try something like #Url.Encode(yourStringObject) and pass it as the last value or as a query (i.e. &q=url)

Using forward slashes ('/') in URI templates without encoding

I've got a RESTful WCF service that acts as a file store. Since there can be any number of directories and sub-directories, I'm trying to let the users access them by simply putting the file path into the URL. Is there a way I can do this without requiring the user to encode the slashes?
For example, what I want is a URI template of Files/{path} that can be accessed like http://localhost:8000/Files/folder1/subfolder2/subfolder3/file.jpg.
You can put a * at the end of your uri template. Inside your operation you can interpret the rest of the uri that matches to * as your file path.
you'll need to use URL rewriting techniques to accomplish this. if using .net 4 use Route table.
I'm afraid not understanding right. Would the following work?
Let the user enter something like folder1/subfolder2/subfolder3/file.jpg in path variable.
Perform: String encodedPath = path.Replace("/", "%2F");

Categories

Resources