Simple server and client request/response in C# - asp.net

OK. I just want to know if this can be done.
I'm using C# asp.net language.
I want to send a structure to a webserver from a webpage (much like you you can pass a structure to a function). The server would act on the data found in the structure, and then return back the structure (with its contents modified) to the "client" webpage.
The webpage would then, based on the data received, do a particular task.
is it possible that information be passed between webpages & servers just like functions?
If not, is there any way that i may be able to accomplish what was said above?

Answer is yes, you can try calling a server-side function from a client-side script.
Here are a few good reads:
Client-Side Web Service Calls with AJAX Extensions
Client Side Callbacks in ASP.NET 2.0
Extend ASP.NET AJAX Client-Side Function - The Server-Side Way
ASP.NET 2.0 Client-Side Features: Bring Server-Side Power to the Client

Sure. You can encode your structure in a string, and then send it to the server as a query string or in a form POST, using either a dedicated link or plain JavaScript or Ajax (XMLHTTP). When the response arrives, you can similarly decode it with JavaScript and then take whatever action is appropriate.
There are several libraries to help automate this kind of thing, including XML-RPC.

Related

When and how to use POST, PUT, and Delete method in Web API

I am new to Web API and the tutorial I was watching uses Fiddler to sniff and send request.
My question is how to use the post, put and delete methods without using fiddler and where are these usually used in the when creating in software development? Fiddler allows you to compose a request body how about the browser though?
I don't seem to understand why you are only able to use these on a sniffing tool. I understand that I am most probably speaking out of ignorance.
(This answer is not specific to ASP.NET)
To avoid any confusion, I'm assuming you understand this much about RESTful webservices:
If you are creating a new entity, use POST.
If you are reading an entity, use GET.
If you are updating an entity, use PUT.
If you are deleting an entity, use DELETE.
As you noticed, only the GET and POST methods can be used in plain web forms. AJAX, however, supports using any HTTP method. Therefore, to use the PUT and DELETE methods, send your data to the server using JavaScript methods such as XMLHttpRequest, the newer Fetch API or a convenient wrapper like jQuery's $.ajax. (There are many tutorials for whichever you pick.) For example, if you have a form of updated values, replace your input button with a button element that has a JavaScript click event handler. The same principle applies to a delete link, swap the href for a click event handler that makes an AJAX request to your server.
If you simply want to create requests using some tool, then you can use tools like Fiddler, Postman, curl, etc. - as you have stated, Fiddler can also be used to monitor as well as create requests.
If you want to go beyond that,
you have HttpClient, WebClient to build requests on your own, and for testing.
If you want to get "full control" then look into HttpWebRequest.
VS debugging tools will allow you to inspect the full flow - from request to response - using things like breakpoints, watch, etc.
See previous/other answers if you want to build/create requests client-side (Javascript / XMLHttpRequest)
Hth.

Securing web method on a public web app

I have a public web app that calls ASP.NET web method in an ASMX file, located in the same site folder in IIS. But, to prevent bots to ping the service directly, we'd like to secure the API so that only our HTML 5 client page can access it. Do you have a suggestion on how should I implement this kind of thing ? Not too much, just a simple mechanism that won't take a week of testing please. Doesn't have to be a 100% full proof method since it is public data and the API just pumps data out, not inserting anything. Just something to limit possibilities of DDOS attack on the API.
The way I've tackled this in the past is with a custom header.
Essentially if your web page is using some form of AJAX call to call back to your services layer, then you can use something like:
xhr.setRequestHeader('custom-header', 'value');
where 'xhr' is an XML Http request that you've built in Javascript
of course you could also take the much easier route of just adding a parameter to your calls query string, EG:
in your ajax call, request:
http://my.services/service.asmx?somesecretkey=foobar
instead of just
http://my.services/service.asmx
Then you can just use the request's query string collection server side to see if it's present or not, and refuse the connection if it's not.
You could even go so far as providing some seed value in the data passed to the page in the first place, then use that seed value to create a unique value (one the server can also calculate) that is returned back in your request to the server.
Doing it that way would provide a slightly higher level of security, as the values would be semi random and not easy for a bot to guess.
Bear in mind also, that if you control the calling page, and you are doing this by ajax, you can also put this key in your post variables collection too so it doesn't have to be visible in the get request.

post array of values to a http web service via ajax and read on asp.net web server

I am just wondering how to do this:
I have a page which shows some tabular data. On each row there is a checkbox. The user selects two/more checkboxes; my web page must find the corresponding ids of the rows which are checked. I must send this to the http web service. (basically a *.ashx file). I have two questions:
how will I access this on web server
how will my web server know the total no. of items I've passed. Must I manually send this to the server as a http post parameter?
what if the data I am sending is more complex like a person object which has fields like FirstName, LastName, etc. ?
I know I can use a asp.net web service or a wcf service with exposes a datacontract. But what is the best way to handle this without the SOAP overhead; what if you are sending all this data via ajax as a normal http post?
http://jquery.malsup.com/form/#getting-started might be worth investigating as a starting point.
In terms of your questions, the answers will partly depend on whether you are using MVC or WebForms. But in general:
If you use the above plugin, you will access it just like it was a normal form submit (post).
They will be populated into Request.Form.
This shouldn't be a problem. Model binding on MVC should handle this fine, I am not 100% sure of the equivalent on WebForms (you could always do it manually).
My suggestion would be to ignore the AJAX requirement for now. Get it working with a standard form submit (POST). Then come back and integrate the above plugin to enable AJAX.
1. How will I access this on web server
Using either postback (and a checkboxlist control), or handling POST yourself. GET wouldn't be appropriate for the task.
2. How will my web server know the total no. of items I've passed. Must I manually send this to the server as a http post parameter?
It will send back the value of each checkbox you have on the form, which will give you the total.
3. What if the data I am sending is more complex like a person object which has fields like FirstName, LastName, etc. ?
You could use Viewstate for this, or just re-assemble the objects on the server. Typically this means putting an ID in the checkbox value for the person each checkbox represents - you don't need to send back every piece of information about the object.
What is the best way to handle this without the SOAP overhead; what if you are sending all this data via ajax as a normal http post?
jQuery and JSON would be the best solution for this, if you want side step the built in ASP.NET ajax controls. As suggested, jQuery is the simplest Javascript library for the client side part of the task - you'll need to use $.post

Which one to use Http Handler or Http Module

We want to do something like we have to execute some piece of code in each request to the application. We want to use this same code in multiple applications.
What this code will do is, this will check the incoming request and according to some conditions it will decide whether it has to redirect or not.
So while searching i found that we can use either http handler or http module. But i am not sure about which one has to chose in this case? Please give your suggestions.
HttpModule in this case. It sits in the pipeline where you can inspect each and every request.
How To Create an ASP.NET HTTP Module Using Visual C# .NET
http://support.microsoft.com/kb/307996
HttpHandler is altogether different thing. If you implement HttpHandler for existing file types such as .aspx etc, you will have implement what is already implemented by ASP.NET runtime which is beyond the scope of your requirement.

How can I share a variable value between classic asp, .NET and javascript?

I've created an IHttpHandler in .NET C# which returns pieces of html to a classic asp page.
The classic asp page communicates with the IHttpHandler through basic http requests using ServerXMLHTTP in vbscript or Ajax Calls in JavaScript.
Now, I need a way to share a variable which I have in vbscript but not in javascript with the .NET application.
The first bit, sharing a variable between classic asp and .net is not a problem as I can just add it onto the http request. Because the variable is not available in javascript however I can't do this in that case.
I had the idea that I could maybe cache the variable in the .NET application and use the cached version for javascript calls. But for this to work I would need a way to uniquely identify the "client" in .NET...
I tried to add System.Web.SessionState.IRequiresSessionState to my HttpHandler and use the SessionId but this didn't work because every single call to the HttpHandler seems to get a new ID.
Am I thinking the right way? What are my options here?
You could put your value in a cookie, then you could read with javascript and do anything with it, including sending it in a request to the .net app...
How about having a hidden variable on the page in which you can store the value of the variable from your server side vb script of your asp pages.
Then you can use Javascript to query this variable to send across to the Asp.Net handler through your ajax calls.
In the end my solution to the problem was the following:
I send the variables to my webservice along with the normal ws request
I store the variable values in a hash table on the webservice and return the hashkey to the client in a hidden html object (I used a head <meta> element for that)
In the javascript I read that hash value and attach it to the http-request as a header value.
When the webservice sees that hashkey he looks up the respective variable values in the persistent hash table.
Might not be the very best solution but seems to work well in my case...
I'd recommend a cookie as well. You could also load the value from the database, but that'd be overkill.

Resources