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);
}
});
Related
I have a program in which i am experimenting with Ajax.
The general idea is i have an array or Elements where i am doing a jQuery call for each of those elements using jQuery.ajax. Now the server does a Db call for each of those calls.
So the flow is something like below
Client Request 1---->Server call1---->DB--->Server reply1--->Client Response 1
Client Request 2---->Serve call2---->DB--->Server reply2--->Client Response 2
Client Request 3---->Server call3---->DB--->Server reply3--->Client Response 3
Is it possible to have a staggered or incremental response from server for one Ajax call so it would be something like
Client Request 1,2,3---->Server call 1---->DB--->Server call1---><Server call 2---->DB--->Server reply2---><Server call 3---->DB--->Server reply3--->Client Response 1,2,3
I believe what you are looking for is called comet: http://en.wikipedia.org/wiki/Comet_%28programming%29
I haven't seen it used before, but I was researching it because I may need it for one of my own projects. The basic principle of comet is that you send a request to the server and over a long period of time various responses are written back to the client over the same connection.
Note that there are several different ways that you can implement this. Some allow cross-site scripting, some do not. You will need to research which method(s) work best for your requirements.
I have a Node.js Http Server and Socket.IO running for my web app keeping all the clients in sync in real time. I'm pushing node events out from the client and from the node server, both broadcasts and to individual "rooms". This stuff is amazing.
Now, I have a use case where I would like to emit out a node.js event with a few params from c# code behind, inside a web service call.
[WebMethod]
public MyMethod()
{
// emit node.js event
}
I thought about having a simple html page that requests querystring params and has node initialized in it. And c# code behind just does a http web request calling the url with the params? Not sure if that will work or be efficient, but first idea off the top of my head.
Any ideas or recommendations how to approach this one? Currently, I'm limited to .NET 4.0
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.
As part of learning node.js, I just created a very basic chat server with node.js and socket.io. The server basically adds everyone who visits the chat.html wep page into a real time chat and everything seems to be working!
Now, I'd like to have a C# desktop application take part in the chat (without using a web browser control :)).
What's the best way to go about this?
I created a socket server in nodejs, and connected to it using TcpClient.
using (var client = new TcpClient())
{
client.Connect(serverIp, port));
using (var w = new StreamWriter(client.GetStream()))
w.Write("Here comes the message");
}
Try using the HttpWebRequest class. It is pretty easy to use and doesn't have any dependencies on things like System.Web or any specific web browser. I use it simulating browser requests and analyzing responses in testing applications. It is flexible enough to allow you to set your own per request headers (in case you are working with a restful service, or some other service with expectations of specific headers). Additionally, it will follow redirects for you by default, but this behavior easy to turn off.
Creating a new request is simple:
HttpWebRequest my_request = (HttpWebRequest)WebRequest.Create("http://some.url/and/resource");
To submit the request:
HttpWebResponse my_response = my_request.GetResponse();
Now you can make sure you got the right status code, look at response headers, and you have access to the response body through a stream object. In order to do things like add post data (like HTML form data) to the request, you just write a UTF8 encoded string to the request object's stream.
This library should be pretty easy to include into any WinForms or WPF application. The docs on MSDN are pretty good.
One gotcha though, if the response isn't in the 200-402 range, HttpWebRequest throws an exception that you have to catch. Fortunately you can still access the response object, but it is kind of annoying that you have to handle it as an exception (especially since the exception is on the server side and not in your client code).
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.