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
Solved

C# posting to php web service that expects paramters

Posted on 2014-04-06
10
547 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 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Directions API to Map URL -C#? 3 37
Cant save 3D 4 22
How to parse the string and split the sub directory names in asp.net 7 54
IEnumerable<T> to a List<T> 8 36
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

791 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