Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# posting to php web service that expects paramters

Posted on 2014-04-06
10
Medium Priority
?
576 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Put Machine Learning to Work--Protect Your Clients

Machine learning means Smarter Cybersecurity™ Solutions.
As technology continues to advance, managing and analyzing massive data sets just can’t be accomplished by humans alone. It requires huge amounts of memory and storage, as well as high-speed processing of the cloud.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

618 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