Solved

C# posting to php web service that expects paramters

Posted on 2014-04-06
10
551 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 143

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 143

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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 143

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 143

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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 article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

696 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