flex 3: possible to send array in URLRequest? - apache-flex

In efforts to allow users to save their progress in my application, I've decided to allow them to save. In order to do this, I'd like to create an array with all the necessary information, and send that information to a coldfusion (.cfm) file and process the information from that page. Is it possible to send an array instead of a bunch of url variables? It is possible (and quite probable) that users would exceed the query string length of most browsers.

Yes, just use a post method instead of get. In ColdFusion this will come through the form scope instead of the url scope.
var request:URLRequest = new URLRequest(your-cf-page);
request.data = yourURLVariablesObject;
request.method = URLRequestMethod.POST //this is the important part
urlLoader.load(request);
Put your info in the URLVariables like you usually do...
yourURLVariablesObject.whatever
will become
#form.whatever#
on CF
Just a note... if you really want to make this work well, I'd consider using AMF and an RemoteObject. ColdFusion has the advantage of being able to directly talk to Flex via AMF.
var yourService:RemoteObject = new RemoteObject("ColdFusion");
yourService.source = "yourCFFiles.yourCFC";
Now you can call any method in yourCFC

Related

Consume Web Service, Serialize XML response, save xml to database

I have this situation, I am creating a Web Service in C# where I need to consume a SOAP Web Service, which gives me an XML response back, I need to serialize this XML response and save it to a table in the database.
I have tried the to call the XML in Postman and it worked fine with a
200 OK status
, but I need how to serialize this reponse and save it to the database.
And then I have tried to write this:
public void CreateFilter(Student student)
{
var XML = XmlSerialization <Student> (student);
ConnectDataBase db = new ConnectDataBase();
SqlCommand cmd = new SqlCommand("sp_Student");
cmd.Parameters.Add("#name", SqlDbType.VarChar).Value = student.name;
cmd.Parameters.Add("#surname", SqlDbType.VarChar).Value = student.surname;
cmd.Parameters.Add("#subject", SqlDbType.VarChar).Value = student.subject;
cmd.Parameters.Add("#student", SqlDbType.Xml).Value = XML;
}
Any thoughts on how to get a response from the Web Service I have to consume, and then serialize the response I'm getting back and then save the serialization on the database?
Thank you in advance
Well, you have a couple of options really. Essentially, if you aren't leveraging an ORM (like Hibernate or some such), you want to:
Grab the XML payload
Get that deserialized into an object instance so you can work with it
Pick out the data you are interested in and persist it
Step 2 is essentially writing a class (or tree of classes) that mimmicks the field structure of the XML, then asking a library nicely to parse the XML state into an instance of that class. This then makes it easy to work with for you.
You can either leverage the the native deserialization as per:
https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer.deserialize?view=net-6.0
Or, just for arguments sake alternatively you could use a modern serialization library. There isn't much of a good choice when it comes to XML and C#, so the main one I can think of that will get you from point a to point b is Json.NET:
https://www.newtonsoft.com/json
This library, though predomenantly pushed as an all-in-one object mapper for JSON is also able to translate between XML and Json:
https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm
Depending on how much of a fight the native deserializer puts up (defining the schema can be a bit of a pain, bloody SOAP am I right), it might be easier to use Json.NET to hoover up the XML, convert it to JSON, then deserialize that json as an object.
This gives you a 2-step deserialization process which isn't ideal, but it's not such a bad thing either as you get to work with the "nice" library and not have to fight the old baked in xml serialization stuff.
The choice is yours really. I'd give the first option a good go first then if that puts up too much of a fight you have Json.NET to fall back on :)

Flex: Configure AMF serialization warnings?

I have been trying to test my application to make sure that all the important classes can serialize/reload themselves properly (especially those which implement IExternalizable):
[Test]
public function testMyObjectSerialization():void {
var myobj:MyObject = new MyObject();
var ba:ByteArray = new ByteArray();
ba.writeObject(myobj);
ba.position = 0;
var loadedObj:MyObject = ba.readObject();
assertMyObjectEqual(myobj, loadedObj);
}
And I would like to be warned when I try to serialize a strongly-typed object which does not have a [RemoteClass] set (because that almost certainly represents a bug in my code).
So, is there any way to configure the AMF serializer to give warnings?
Also, it seems like this might be possible using services-config.xml… But the documentation seems to imply that services-config is channel-level, and I'd really like it if my unit tests could run without talking to the server (and I'm not using LCDS, so a bunch of the services-config wouldn't apply to me anyway).
There is no way to configure the native AMF serialization/deserialization from the Flash Player to give you warnings if [RemoteClass] or any other metadata is set or not.
However you can write your own class to do that - you can register all the objects in a list and check for [Remote] using flash.utils.describeType. Or use a wrapper over writeObject which check for the [Remote] metadata.

ASP.NET MVC - Is there a way to simulate a ViewState?

I have the following situation... In a certain View, the user must select the initial hour, the final hour and the weekday. But, I can't save this informations to DB 'cause I need to save my whole page and I need the primary key of the primary table, but that's not the point.
So, while I don't save these data to DB, I'm saving to a Session. I was told to save to a cookie, but it appears that cookies have a size limit. So, I'm saving to a Session.
Buuuut, I was also told that I could save these informations (hours and weekday) to the user page, simulating a ASP.NET ViewState...
Does anyone know how to do this?? Does anyone know how to save temporarily these data withou using cookie or Session??
Thanks!!
Hidden input fields won't help?
<%= Html.Hidden(...) %>
Update (serializing an object to base64):
var formatter = new BinaryFormatter();
var stream = new MemoryStream();
formatter.Serialize(stream, myObject); // myObject should be serializable.
string result = Convert.ToBase64String(stream.ToArray());
When you want to fetch it back:
var formatter = new BinaryFormatter();
var stream = new MemoryStream(Convert.FromBase64String(hiddenFieldValue));
var myObject = (MyObjectType)formatter.Deserialize(stream);
Make sure you validate the data stored in the field when you use it as the client might change it. ViewState takes care of this automatically.
Side note: ASP.NET uses LosFormatter instead of BinaryFormatter to serialize ViewState as it's more efficient or ASCII based serialization. You might want to consider that too.
TempData["MyData"], mind you this will only last one round trip.
You could save a javascript array on the client... and then transmit all the information when the user ultimately saves.
You have to work a little more, but in the end it pays off.
I heavily use jQuery to do stuff like that, it's easier than it seems.
If you just want to save the data for that request and the next request I'd recommend using Tempdata, else I'd recommend using Mehrdad`s answer.

ASP.NET Localized web site -- updating on the fly

I think I have a solution to this, but is there a better way, or is this going to break on me?
I am constructing a localized web site using global/local resx files. It is a requirement that non-technical users can edit the strings and add new languages through the web app.
This seems easy enough -- I have a form to display strings and the changes are saved with code like this snippet:
string filename = MapPath("App_GlobalResources/strings.hu.resx");
XmlDocument xDoc = new XmlDocument();
XmlNode xNode;
xDoc.Load(filename);
xNode = xDoc.SelectSingleNode("//root/data[#name='PageTitle']/value");
xNode.InnerText = txtNewTitle.Text;
xDoc.Save(filename);
Is this going to cause problems on a busy site? If it causes a momentary delay for recompilation, that's no big deal. And realistically, this form won't see constant, heavy use. What does the community think?
I've used a similar method before for a very basic "CMS". The site wasn't massively used but it didn't cause me any problems.
I don't think changing a resx will cause a recycle.
We did something similar, but used a database to store the user modified values. We then provided a fallback mechanism to serve the overridden value of a localized key.
That said, I think your method should work fine.
Have you considered creating a Resource object? You would need to wrap your settings into a single object that all the client code would use. Something like:
public class GuiResources
{
public string PageTitle
{
get return _pageTitle;
}
// Fired once when the class is first created.
void LoadConfiguration()
{
// Load settings from config section
_pageTitle = // Value from config
}
}
You could make it a singleton or a provider, that way the object is loaded only one time. Also you could make it smart to look at the current thread to get the culture info so you know what language to return.
Then in your web.config file you can create a custom section and set restartOnExternalChanges="true". That way, your app will get the changed when they are made.

Getting progress on POST using HTTPService in Flex/AS3

I'm using HTTPService with a POST operation to submit a Base64 encoded file (taken from bitmap data within the app) but I could really do with getting some idea of the progress of the POST operation (e.g. like the FileReference.upload()).
I don't think this is possible, but it would be awesome if it is (via any means, I'm willing to change my setup to get this).
Do not use HTTPService. Use URLRequest, URLLoader, and URLVariables.
If your using an HTTPService tag, get ride of it and replace it with a Script tag filled with something like ...
private function forYou() : void{
var req : URLRequest = new URLRequest("PUT YOUR URL HERE")
var loader : URLLoader = new URLLoader();
var params : URLVariables = new URLVariables();
params.WHATEVER = WHATEVER YOU WANT IT TO BE;
req.data = params;
req.method = URLRequestMethod.POST;
loader.addEventListener(ProgressEvent.PROGRESS, YOUR LISTENER FUNCTION NAME);
loader.load(req);
}
Assign this function name to the creationComplete attribute of the root tag.
If your not using an HTTPService tag, just get ride of the HTTPService object in your actionscript and use the above code.
This worked well for me to consume a REST web service:
http://code.google.com/p/as3httpclient/wiki/Links
Example
This isn't possible with HTTPService. Its only events are result, fault, and invoke (other than the non-relevant inherited events of activate and deactivate).
To get progress information on an upload process, the server would have to provide the information, which would require a means of communication between the server and the client during the operation, which isn't there for a regular HTTP POST operation.
One option might be to create an object on the server that would be instantiated whenever the server began receiving POST data from your client. It would then keep track of the progress and expose that data to the rest of your server-side application. Your client could then initiate a polling system that would request the value of that particular variable.
Seems like kind of a far-fetched option, though...

Resources