Solved

C# posting to php web service that expects paramters

Posted on 2014-04-06
10
536 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
 
LVL 74

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now