Solved

C# posting to php web service that expects paramters

Posted on 2014-04-06
10
541 Views
Last Modified: 2014-04-24
Hi.

I am new to creating web services, and I need to pass back data to a php web service.  I have been reading up on the net on how to do this, but this particular web service exposes parameters, and I am unsure of how to post data to this web service.  Can you advise how this is to be done.

The web service looks similar to this :

http://webserviceip.passdata.php?source=CompanyName&user=username&pass=password&function=addData&Name=Paul&Surname=King

Can anyone assist?
0
Comment
Question by:Jasmin01
  • 5
  • 4
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39982275
well, you just call the url like that, replacing the values (urlencoded) at the placeholders...
I presume you know how to call a plain http/get from c#
0
 

Author Comment

by:Jasmin01
ID: 39982313
Would it be something like this?

String strPostData = "Source=" & strCompany & "username=" & strusername & "pass=" & strpassword

                    //string url = "http://webserviceip.passdata.php";

                    HttpWebRequest myHttpWebRequest = HttpWebRequest.Create("http://webserviceip.passdata.php");
                    myHttpWebRequest.Method = "post";
                    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39982333
yes, plus:
byte[] byteArray = Encoding.UTF8.GetBytes (strPostData);
myHttpWebRequest.ContentLength = byteArray.Length;
Stream dataStream = myHttpWebRequest.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();

Open in new window


see:
http://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39983701
Would it be something like this?
Er... Maybe. It depends on how your PHP service is defined. Your original code sample showed a GET operation. Your C# code is showing a POST. If the service is actually expecting a GET, then you can removed lines 6 & 7 in your C# code, and add the parameters to the URL in line 5.
0
 

Author Comment

by:Jasmin01
ID: 40019457
Hi.

I have removed lines 6 and 7, but do I still need :

byte[] byteArray = Encoding.UTF8.GetBytes (strPostData);
myHttpWebRequest.ContentLength = byteArray.Length;
Stream dataStream = myHttpWebRequest.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
0
 

Author Comment

by:Jasmin01
ID: 40019486
I am now getting the error of :  ERROR: Invalid Source - ERROR: Invalid Source.

What could be going wrong?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40019561
reading like this, that error seems to come from the php web service... not sure how to troubleshoot that..
0
 

Author Comment

by:Jasmin01
ID: 40019594
Thank you.  It turns out that I was passing the parameter "Source", when it required "source".

Silly typo, but it does work now.

Just one more question, the web service returns a result with an ID number and the date that my data was posted via that web service.  How would I catch that information to store on my database.  The result seems to be returned as one long string.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40019612
I think that would be another question, where you should put the data/string you receive and what to "extract" from it (I assume you know already how to store data in general in a database)
0
 

Author Closing Comment

by:Jasmin01
ID: 40019785
Thank you
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question