Upload image from android to ASP.NET server C# - c#

After searching in google and here, i saw the popular approach is to convert the image to byte array and then to base64 string. this part was easy, but sending it and receiving it over HTTP is harder, and I can't find an easy way to do it.
I have 2 main question which depend one another
Send with android on HTTP:
Android 6 deprecated HttpClient, so i don't want to use that.
I thought to use Volley but i cant figure out how to make it work right.
Can you please give me an example of code to transfer it in a simple and elegant way which will be easy to intercept with C#?
Receive with C# and use of web service:
I'm not sure what is the best way to implement it? Should I create a web service method? in case i should, how can i intercept the post request? Should I create a new page to handle only this part. this way i know how to handle the request.
Edit:
I managed to create a request using HttpClient, but the base64 string after converting the file made the URI too long.
any other ideas?

I found here a post for uploading files from Android to ASP.NET Web API. However the HTTPClient was used for handling HTTP request but I think you can use code as reference.

Found the simple and elegant solution i wanted!
I use loopj library, "android-async-http" for sending files with 3-4 lines.
Then i get the request to a new aspx file in my ASP.NET server, and save it using "Request.Files" object.

Related

Unknown WebService description and consume it from C#

I'm quite a noob and have been developing 'seriously' (as my job) from a couple of months barely, so i apologize in advance for my ignorance.
There is this web service that i need to consume from a C# client on an aspx page I want to develop, but first I need to understand the webservice, I'm not sure which language it's made upon but I think it's PERL, since the web service's URL is like this "http://wschsol.mideplan.cl/mod_perl/xml/fps-by-rut". This webservice was developed by other people which i cannot contact right now and is running on a linux server to which i don't have access either.
The webservice's job is pretty simple, it receives a person's national ID number and returns some information about him on xml format, which i want to show on my client aspx page with some grids and stuff.
I have read around the internet that it's possible to see a description of a webservice and its methods using the WSDL variable after the common ".asmx" extension, but in this case there is no extension and so, i can't use the the ?WSDL. I'm guessing that maybe "fps_by_rut" is only a webmethod, and not the webservice itself. So the question is: how do I use the webservice?
Since I know what kind of request is expected (a person's ID), I tried to manually add an ID to the URL through the browser (like this: "http://wschsol.mideplan.cl/mod_perl/xml/fps-by-rut?rut=6985462-1") and if I do it responds normally in xml format.
I tried to add a web service reference for it on my project, but well, i pasted the whole URL and when I click "go" it says it needs credentials. I have these credentials, a user and password, but they are not working... what confuses me is that there is another client to this same webservice programmed on classic asp made by the guy before me here, and i can acces that code, and when i see the line on which he calls the web service it's like this:
xml.Open "GET", "http://wschsol.mideplan.cl/mod_perl/xml/fps-by-rut?rut="&rsVac(0), False,"user","password"
i have censored the "user" and "password" strings since those are the actual credentials. This classic asp client works fine with those credentials. I tried to use those when creating the reference, but they are not working. Even more, when i manually added the ID through the browser it asked me for credentials and they worked too...
Am I going the wrong way? Please guys, i need guidance. If there is a course out there which I can read that helps me understand all of this webservices stuff, i'd be hugely grateful. Or if someone can tell me which way to go, I'm pretty sure I'm in the wrong direction...
Thanks in advance for any help!!!
WSDL is used for SOAP and we both don't know if it's an SOAP-Service.
You should just use a HttpClient and make your Get-Calls to the API.
You can use something like this:
var client = new HttpClient();
client.BaseAddress = new Uri("http://wschsol.mideplan.cl");
var httpResponse = await client.GetStringAsync("mod_perl/xml/fps-by-rut?rut=<InsertParamHere>");
Edit Authorization:
You have to add this:
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
Snippet source: Simple C# .NET 4.5 HTTPClient Request Using Basic Auth and Proxy

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.

Why I am getting this error with Request.Url.PathAndQuery?

As I have been requested by my instructor to use AntiXss library in the development of my senior project, I am facing a lot of difficulties of using this library because of the lack of resources on the web. A part of my project I have an upload file function where the user will be able to upload files, and after uploading his files, he will be redirected to the same page to see some other information. Everything works fine, but when I added AntiXss library and use it with the following line only, I got this error
(HTTP 400 Error - Bad Request)
and I don't know why. Could anyone tell me why I am getting this error? And how to fix it?
C# Code:
Response.Redirect(Encoder.HtmlFormUrlEncode(Request.Url.PathAndQuery));
Break up your code and look at each step:
Take the incoming request's URL, and extract out the path and query.
Run that through a form-based encoder
Redirect to that string
What do you think a form-based encoder would do in order to prevent XSS attacks?
Try this:
Response.Write(Encoder.HtmlFormUrlEncode("http://www.stackoverflow.com"));
What is written out? Try putting that in a web browser, and you'll likely get a 400 (or a 502) error.
Request.Url.PathAndQuery
The above syntax returns `/Cambia3/Temp/Test.aspx?query=arg`
For further url references check this
HtmlFormUrlEncode gets string and encode as parameters. for further info on that see here

how to create a win app which can interact with a website?

Hi
I'm trying to write a windows application which can read HTML content of a specific website and fill some data in some input fields and submit the page.
what I did untill now was reading page content from a WebBrowser object, navigated to the website.
I know that i need to create some request/response variables and work with them, but I have no good view on what i'm trying to do.
also, my information about HttpRequest and HttpResponse is so low...
HttpWebRequest is what you're looking for, examples for read/write to scrape abound.
WebClient might be simpler for you to implement but in the end is only a wrapper to the above.
You can try to use the approach described in the HttpWebRequest with https in C# thread as a starting point.

Getting data from a webpage

I have an idea for an App that would really help me out in work but I'm not sure if it's possible.
I want to run a C# desktop application that will ask for a value. When a value is supplied, the application will open a browswer, go to a webpage and add the value into a form on an online website. The form is then submitted and a new page is loaded that contains a table of results. I then want to extract the table of results from the page source and write code to parse the result values.
It is not important that the user see's this happen in an actual browser. In other words if there's a way to do it by reading HTTP requests then thats great.
The biggest problem I have is getting the values into the form and then retrieving the page source after the form is submitted and the next page loads.
Any help really appreciated.
Thanks
Provided that you're only using this in a legal context:
Usually, web forms are sent via POST request to the web server, specifically some script that handles it. You can look at the HTML code for the form's page and find out the destination for the form (form's action).
You can then use a HttpWebRequest in C# to "pretend you are the form", sending a POST request with all the required parameters (adding them to the HTTP header).
As a result you will get the source code of the destination page as it would be sent to the browser. You can parse this.
This is definitely possible and you don't need to use an actual web browser for this. You can simply use a System.Net.WebClient to send your HTTP request and get an HTTP response.
I suggest to use wireshark (or you can use Firefox + Firebug) it allows you to see HTTP requests and responses. By looking at the HTTP traffic you can see exactly how you should pass your HTTP request and which parameters you should be setting.
You don't need to involve the browser with this. WebClient should do all that you require. You'll need to see what's actually being posted when you submit the form with the browser, and then you should be able to make a POST request using the WebClient and retrieve the resulting page as a string.
The docs for the WebClient constructor have a nice example.
See e.g. this question for some pointers on at least the data retrieval side. You're going to know a lot more about the http protocol before you're done with this...
Why would you do this through web pages if you don't even want the user to do anything?
Web pages are purely for interaction with users, if you simply want data transfer, use WCF.
#Brian using Wireshark will result in a very angry network manager, make sure you are actually allowed to use it.

Categories

Resources