How do I invoke a PHP script on a Web server? - c#

Apache server is installed in one machine and there is a .php script present in the server. Now from my win32 or c# application how do I invoke the script and how to receive the data from the server?

Its the same as reading output from any web page, the php script is processed by the server
This code reads the output of a php page from the php.net online manual:
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(#"http://www.php.net/manual/en/index.php");
using (HttpWebResponse resp = (HttpWebResponse) wr.GetResponse())
{
StreamReader sr = new StreamReader(resp.GetResponseStream());
string val = sr.ReadToEnd();
Debug.WriteLine(val);
}

You need to open a network connection to localhost, or use the php command-line interpreter, but I'm not sure if that is linux-only, it should work for windows... try php (filename.php) to execute and return the echoed output.

Related

Xamarin Android app works on emulator, crashes on real device when using WebRequest

I am using this example to get some data from a link to an Android device:
https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.110).aspx
The app works fine when using the emulator (Nexus 5 with Android Kit Kat), but when I deploy it (release version) on an actual Android (Samsung S3 mini with Jellybean) the app starts, but crashes after that with "Unfortunately .. has stopped working". If I don't use WebRequest.Create, all my other components in the app work everywhere.
WebRequest request = WebRequest.Create(
"MY LINK HERE"); //with a real link ofcourse
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd()
reader.Close();
response.Close();
Enable Internet permission for App in AndroidManifest.xml file under Properties as follows:
<uses-permission android:name="android.permission.INTERNET" />
or enable through wizard from following steps:
Right click over project and Properties
Under Android Manifest
enable checkbox for Internet under Required Permissions
Hope this helps
I fixed the ussue. I didn't know that I had to set permission to use internet.
Project->Project Options->Build->Android Application->Required permission-> Tick the Internet checkbox and press OK.

Best way to create python web service and c# client application

I am looking for a simple and reliable way to create Python Web Service and consume it from the .Net (c#) application.
I found plenty of different libraries, where one is better than another, but nobody seems to have a complete working example with Python Web Service and some simple c# client. And reasonable explanations of steps to configure and run
I am suggesting using Tornado. It is very simple to use, non-blocking web-server written in Python. I've been using it in the past and I was shocked how easy it was to learn and use it.
I am strongly encouraging you to design your API with REST in mind. It will make your API simple, and easy to consume by any language/platform available.
Please, have a look at the 'Hello World' sample - it has been taken from Torando's main site:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
As for the client part - there is nothing complicated:
string CreateHTTGetRequest(string url, string cookie)
{
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.Headers.Add("Cookie", cookie);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();
reader.Close();
response.Close();
return content;
}
In case the server is running on your local machine, the URI is: 'http://localhost:8888/'
you may start your practice by:
Install ZSI
Create a WSDL for your service
The full example
4.On client(C#) follow this tutorial

can asp.net run without .net framework

i am very new to asp.net. I would like to ask can asp.net run without the .net framework? as in can default.aspx run smoothly without the .net framework? I am asking this due to the following existing code which was runned on a web hosting server and another is a private server. I am not sure about the private server details ( going to know in a 2-3 days)...the code goes as...
try
{
WebRequest req = WebRequest.Create(General.SiteUrl + "/pages/" + page + ".htm");
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader reader = new StreamReader(stream);
content = reader.ReadToEnd();
}
catch { content = "<html><head></head><body>Content not found.</body></html>"; }
the web hosting server manage to run the "Try" successfully whereas the private one always shows content not found....any ideas guys?
People that visit your website will not need the .NET Framework; all they'll need is a browser.
The server that runs your website will need the .NET Framework since ASP.NET is a part of it.
The .NET Framework is required on the Server side for a few reasons (these are just some examples):
Your code is compiled into an intermediate language designed to be platform agnostic. A runtime (The .NET Framework) is required to convert this intermediate language into something the machine can understand. This is accomplished by the JIT.
There are several libraries in ASP.NET; System.Web.dll; for example. These are distributed as part of the .NET Framework.
The code is hosted inside of a virtual machine (in the non-traditional sense). The virtual machine takes care of a lot of heavy lifting for you; such as security; garbage collection; etc. Again; this is all part of the .NET Framework.
EDIT:
I think you are asking the wrong question here. You ask wondering why your code is going inside of the catch block and returning Content not found. The .NET Framework is properly installed since the catch block is being called; in fact it couldn't get nearly that far without the .NET Framework.
You need to figure out what exception is being thrown inside of the try block that is causing it to go into the catch block. You can achieve this with a debugger; logging; or temporarily removing the catch block all together to get the server to let the exception bubble all the way up to the top. For example; if you change your code block to look like this:
WebRequest req = WebRequest.Create(General.SiteUrl + "/pages/" + page + ".htm");
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader reader = new StreamReader(stream);
content = reader.ReadToEnd();
The exception details will be displayed in the browser (provided you have debugging turned on). What error is displayed without the try / catch?
No, .Net code will not run without support of the .Net framework. Because code written in .Net language will be compiled and converted to IL (Intermediate Language) Code.
The .NET framework, or some variation of, e.g. Mono, is not required on the client side. This is a requirement of the server which is serving the pages.
When data is sent to the client via HTTP, it is translated into HTML. So all the client would need would be a browser capible of consuming HTML and running any scripts associated with that site.
the .net framework is the foundation that powers this code
try
{
WebRequest req = WebRequest.Create(General.SiteUrl + "/pages/" + page + ".htm");
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader reader = new StreamReader(stream);
content = reader.ReadToEnd();
}
catch
{
content = "<html><head></head><body>Content not found.</body></html>";
}
so in short, "no", you must have the .net framework installed on the server that is hosting your website.
On the other hand however, on the client side, your website visitors do NOT need the .net framework to "view" your website.

Using the php engine inside a c# application

I would like to give my users the ability to configure some php script to interact with my applycation.
I would like to give the user a Memo. The user writes some code with a php syntax, then press execute. The php engine executes the code, so I can print a result.
For example I would like to write something like:
PHPassembly = Assembly.LoadFrom("php5ts.dll");
ExecutePHPScript(PHPassembly,StringContainingAScript);
ResultVar=GetPHPVar(PHPassembly,"ResultVar");
I don't have a web server. I don't have an internet connection. Only a local application for windows.
I have tryed to load the php5ts.dll, but the compiler says that I need an assembly manifest.
Someone knows how to interact with php?
You need 2 files (from php-5.3.5-Win32-VC9-x86) php-win.exe and php5ts.dll
Than just place those 2 files in you executable directory and run:
string code = "echo 'test';";
System.Diagnostics.Process ProcessObj = new System.Diagnostics.Process();
ProcessObj.StartInfo.FileName = "php-win.exe";
ProcessObj.StartInfo.Arguments = String.Format("-r \"{0}\"", code);
ProcessObj.StartInfo.UseShellExecute = false;
ProcessObj.StartInfo.CreateNoWindow = true;
ProcessObj.StartInfo.RedirectStandardOutput = true;
ProcessObj.Start();
ProcessObj.WaitForExit();
string Result = ProcessObj.StandardOutput.ReadToEnd();
MessageBox.Show(Result);
You can try hosting the php runtime locally. There are preconfigured packages for that, with PHP + Apache, like xampp and WampServer. This way, you can call it via HTTP requests to localhost (such an approach is discussed here).
With a bit of configuration,you could execute your code against php running on the command line.
See php.net for more info
in search of something similar myself I found this question.
maybe port and include code from http://ph7.symisc.net/index.html if you don't need too fancy php stuff?

I want to kill a few ClickOnce applications - can I access a ClickOnce "installation folder URL" from code?

I would like to have an offline ClickOnce application (they can run it from Start menu), but I would like my application to function similar to an online one (make sure the web page / server is there to run). This way I can take off (uninstall) a ClickOnce application, and it will stop working for end users without having to go to 1000's of desktops. This is for an internal corporate environment, so we have total control over the servers, end clients, etc.
There are a lot of clients out there world wide. Essentially, I would like to give them a message like "This applications functionality has been moved to XXX application, please use it instead." Or "This application has been retired." If I could get the install folder URL from code, I could have a message.xml file sitting in that directory that would have some logical tags in it for accomplishing this. If that message isn't there (server offline) I could have the application fail gracefully and instruct the user to contact their local IT for assistance.
Or can this same thing be accomplished in a different way?
I've used the following code to solve part of your problem:
try
{
// The next four lines probe for the update server.
// If the update server cannot be reached, an exception will be thrown by the GetResponse method.
string testURL = ApplicationDeployment.CurrentDeployment.UpdateLocation.ToString();
HttpWebRequest webRequest = WebRequest.Create(testURL) as HttpWebRequest;
webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
// I discard the webResponse and go on to do a programmatic update here - YMMV
}
catch (WebException ex)
{
// handle the exception
}
There may be some other useful exceptions to catch as well -- I've got a handful more in the code I use, but I think the rest all had to do with exceptions that the ClickOnce update can throw.
That handles the missing server case -- though it does require you to have been proactive in putting this in place well before the server is retired.
You can only get the deployment provider URL if the application is online-only. Otherwise it's not available.
Aside from moving a deployment, you can programmatically uninstall and reinstall the application.
You deploy the new version (or whatever you want to install instead) to another URL. Then you add uninstall/reinstall code to the old version and deploy it. When the user runs it, he will get an update, and then it will uninstall itself and call the new deployment to be installed.
The code for uninstalling and reinstalling a ClickOnce application can be found in the article on certificate expiration on MSDN, Certificate Expiration in ClickOnce Deployment.
You could create a new version of your application which only contains a messagebox saying "This application is retired" and deploy it.
The next time a user starts the application the new version will be downloaded showing your messagebox.
What I did was to combine the comments on this question, and some sprinkling of my own to come out with this answer below.
XML Document saved as an HTML (our web servers don't allow XML transfers):
<?xml version="1.0" encoding="utf-8"?>
<appstatus>
<online status="true" message="online"/>
</appstatus>
Then I have the following code read from the above, and use it to see if the application should close or not:
string testURL = "";
try
{
// Probe for the update server.
// If the update server cannot be reached, an exception will be thrown by the GetResponse method.
#if !DEBUG
testURL = ApplicationDeployment.CurrentDeployment.UpdateLocation.ToString() + "online.html";
#else
testURL = "http://testserver/appname/online.html";
#endif
HttpWebRequest webRequest = WebRequest.Create(testURL) as HttpWebRequest;
webRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(webResponse.GetResponseStream());
XmlDocument xmlDom = new XmlDocument();
xmlDom.Load(reader);
if (xmlDom["usdwatcherstatus"]["online"].Attributes["status"].Value.ToString() != "true")
{
MessageBox.Show(xmlDom["usdwatcherstatus"]["online"].Attributes["message"].Value.ToString());
this.Close();
return;
}
}
catch (WebException ex)
{
// handle the exception
MessageBox.Show("I either count not get to the website " + testURL + ", or this application has been taken offline. Please try again later, or contact the help desk.");
}

Categories

Resources