Setup Http Post Server - c#

I have a mobile application which I want to call a http post to pass a binary string and write it to a SQL Server. Can you please give me some examples of code in setting up a http post server (Server side code) to accept 2 values (brinary string & DeviveID string).
Any help, advice or links welcome....

I don't know the iPhone side, but from the C# side, you could either do it via HTTP GET variables (e.g. http://www.example.com/?string=foo&devive=bar) and handle your SQL in there.
You could also run a small program that has a listening Socket or TcpListener on whatever port you want, and then have a BeginRead() method active waiting for input from the iPhone app. Once the BeginRead() returns with some data, you could then handle your SQL.

You could create a WCF REST service for this (look up the WCF REST Starter Kit), but as a quick-and-dirty solution you could do something much simpler: Just create an ASP.NET page that processes incoming POST data in its Page_Load handler.
If your POST format is the same one used by browsers (var1=123&var2=456), you can just use Request.Form["var1"] on the page. See http://forums.asp.net/t/1464546.aspx
If your POST format is different (e.g. XML), use Request.InputStream. See http://schlueters.de/blog/archives/31-Manually-processing-HTTP-POST-data-in-an-aspx.html

You could setup a Web Method on the web server to handle the requests from the iPhone app. Then you just send the data as a normal HTTP POST and the web method would handle the data, and call the SQL Server stored procedure.

You should be able to check the Request object to see if the data was posted and then perform your call to SQL Server.
For example:
Request.Params.Get( "sampleParam" )
will return the value of a sampleParam. As long as the posting application, page, or device posted the data you are expecting you will be able to get to it.

Related

Proprietary bare-bone ASP web service

I want to create a proprietary minimal / bare-bone / data-light webservice. I prefer not to use the standard solutions like Restful, WebAPI, SOAP, WCF, etc.
I just want a web server listener that can receive and process 1 UTF8 string (own message format) and respond with 1 UTF8 string as the result.
My question is: can you give me a starting point, by providing a code example of the interface. Of course not the complete implementation.
Data transfer has to be as minimal as possible (no avoidable headers).
NB: Server language has to be .NET. Code example may be in C# or VB.
The most bare-bone thing to create a web service would be an HTTP Handler.
The sample I linked returns HTML but you could send back XML as well (or anything else really, just make sure to return an appropriate Content Type).
The call to your method would be a regular HTTP call of the URL of your Handler.

Android HTTP to C# Server

Using HTTP I want to send simple string data through android to a C# .NET server. This link suggests using OKHTTP which looks great, but I'm not sure how this would talk to a C# server as I will need a 'connection' where I can send data back to the android phone.
OKHTTP seems to manage drops in connection elegantly according to the website, which is fantastic because I need this kind of persistance, but I'm not sure how I would implement the C# side.
Does anyone know a method of accomplishing this?
It sounds as though you want to do a normal HTTP POST to your server. It's the same thing that would happen when you fill out a form on a web-page and submit the data to a server. If it's a normal kind of webserver it should have full support for receiving a POST and returning a response as well. Are you writing the .NET web service as well?
As for client side technology to use: OkHTTP is a greaty drop-in class for making HTTP requests to a server, but if you plan to do many of them you should also look into wrapping the actual HTTP client into an API that takes care of asynchronous callbacks and things like that. You don't want to be doing HTTP requests on the UI thread and it's boring and error prone to wrap all such calls in AsyncTasks or similar. Take a look at AndroidAsyncHttpClient:
"An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing."
(As a sidenote AndroidAsyncHttpClient might get support for using OkHTTP instead of the default Apache HttpClient)
POSTing to a server is as simple as this:
RequestParams params = new RequestParams();
params.put("A_KEY_TO_IDENTIFY_YOUR_STRING", "THE_STRING_YOU_WANT_TO_SEND");
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://www.yourserver.com", params, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
// handle your response from the server here
System.out.println(response);
}
});

How do you handle an Async Response from a Web Service

I was given the task of creating a web based client for a web service.
I began building it out in c# | .net 4.0 | MVC3 (i can use 4.5 if necessary)
Sounded like a piece of cake until I found out that some of their responses would be asynchronous. This is the flow ... you call a method and they return a response of ack or nack letting you know if your request was valid. Upon an ack response you should expect an async response with the data you requested, which will be sent to a callback url that you provide in your request.
Here are my questions:
If I'm building a web app and debugging on localhost:{portnum} how can I give them a callback url.
If I have already received a response (ack/nack) and my function finishes firing isn't my connection to the client then over ? How would I then get the data back to the client? My only thought is maybe using something like signalR, but that seems crazy for a customer buy flow.
Do I have to treat their response like a webhook? Build something separate that just listens and has no knowledge of the initial request. Just save the data to a db and then have the initial request while loop until there is a record for the unique id sent from the webhook.... oye vey
This really has my brain hurting :-/
Any help would be greatly appreciated. Articles, best practices, anything.
Thanks in advance.
If you create your service reference, it will generate a *ServiceMethod*Completed delegate. Register an event handler on it to process your data.
Use the ServiceMethod_Async() method to call the service.
The way I perceived your question is as follows, though please correct me if I'm wrong about this:
1) You send a request to their endpoint with parameters filled with your data. In addition, you send a:
callback url that you provide in your request. (quoted from your question)
2) They (hopefully) send an ack for your valid request
3) They eventually send the completed data to your callback url (which you specified).
If this is the flow, it's not all that uncommon especially if the operations on their side may take long periods of time. So let's say that you have some method, we'll call it HandleResponse(data). If you had originally intended to do this synchronously, which rarely happens in the web world, you would presumably have called HandleResponse( http-webservice-call-tothem );
Instead, since it is they who are initiating the call to HandleResponse, you need to set a route in your web app like /myapp/givemebackmydata/{data} and hook that to HandleResponse. Then, you specify the callbackurl to them as /myapp/givemebackmydata/{data}. Keep in mind without more information I can't say if they will send it as the body of a POST request to your handler or if they will string replace a portion of the url with the actual data, in which case you'd need to substitute {data} in your callback url with whatever placeholder they stipulate in their docs. Do they have docs? If they don't, none of this will help all that much.
Lastly, to get the data back on the client you will likely want some sort of polling loop in your web client, preferably via AJAX. This would run on a setInterval and periodically hit some page on your server that keeps state for whether or not their webservice has called your callback url yet. This is the gnarlier part because you will need to provide state for each request, since multiple people will presumably be waiting for a callback and each callback url hit will map to one of the waiting clients. A GUID may be good for this.
Interesting question, by the way.

Twilio SMS sandbox using C#

How do you use the Twilio sandbox mode with C#? I have a ashx.cs file that I am using to write my code. Would I put it there? If so, what does that look like?
There is no real great examples on their website on how to do this except for CURL and Ruby.
We are using TwiML to general an XML file tha t parses our data to send back and forth to the Twilio service. We don't want to be charged every time we send a test text message.
How would we set the Sandbox up so we could do some testing.
I found the Test auth Token and account Sid, but how do I use those?
We don't have them in our current application and we are specifying our .ashx page in Twilio to process our code.
Thanks in advance.
Twilio evangelist here.
So if you just want to test that your ASHX handler is generating the right results the easiest way to do this is to just fake a POST or a GET request to that handler. This lets you simulate the GET or POST request that Twilio will make to your app when it gets a text message.
You can see the parameters that Twilio will pass to your app when it receives a text message here:
http://www.twilio.com/docs/api/twiml/sms/twilio_request#synchronous
There are a whole bunch of ways to simulate these requests. cURL is one of them. If your ASHX is expecting query string values, you can also just load the ASHX directly in the browser and append those values in the URL. If the handler is expecting a POST request, I used a chrome plugin called Simple REST Client to make these.
Of course you can also Fiddler to make just about any HTTP request.
The Test Credentials really are more for simulating the use of the REST API (programatically sending SMS messages). I just wrote a blog post that shows how to use the test credentials to create integration tests:
http://www.twilio.com/blog/2013/05/automating-twilio-integration-tests-with-test-credentials.html
Hope that helps.
Devin

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