Solved

Calling a RESTful WCF Service

Posted on 2011-03-15
6
1,663 Views
Last Modified: 2012-06-27
Dear Experts,

I am trying to invoke a RESTful WCF service programmatically.

This is the OperationContract method.

 [WebInvoke(Method = "POST", UriTemplate = "submitPaymentToBankURI")]
[OperationContract]
Stream submitPaymentToBank(Stream info);

This is the client code:

 string baseAddress = "http://localhost:1287/CreditCardWebServiceRESTSite/CreditCardServiceREST.svc";

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(baseAddress + "/submitPaymentToBankURI");
            req.Method = "POST";
            req.ContentType = "text/plain";

            Stream reqStream = req.GetRequestStream();
            string inputData = "MyData";
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            byte[] DataToSend = encoding.GetBytes(inputData);
            reqStream.Write(DataToSend, 0, DataToSend.Length);
            reqStream.Close();
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Console.WriteLine("Client: Receive Response HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);

            StreamReader loResponseStream = new StreamReader(resp.GetResponseStream(), encoding);
            string lcHtml = loResponseStream.ReadToEnd();
            Console.WriteLine(lcHtml);
            Console.ReadLine();

I am using WebHttpBinding.

I am getting an exception on this line:
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Exception Details:
System.Net.WebException was unhandled
  Message="The remote server returned an error: (400) Bad Request."
  Source="System"
  StackTrace:
       at System.Net.HttpWebRequest.GetResponse()
       at TestCreditCardWebServiceREST.Program.Main(String[] args) in C:\MyProject\TestCreditCardWebServiceREST\Program.cs:line 61
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

Any clues on how to fix this?

Thanks in advance for your help.
0
Comment
Question by:shekhar_shashi
  • 4
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
DarrenD earned 500 total points
ID: 35149800
0
 
LVL 3

Author Comment

by:shekhar_shashi
ID: 35151233
Hi Darren,

Yes, I have tried this link before. I can get the my code (and also the code in the link) to work with HTTP Get but not with Post.

Thanks
0
 
LVL 18

Assisted Solution

by:DarrenD
DarrenD earned 500 total points
ID: 35160706
Hi,

How about this?

[OperationContract]
    [WebInvoke
        (Method = "POST",
        RequestFormat = WebMessageFormat.Xml,
        UriTemplate = "/SetGasPrice/For/ZipCode/{zipCode}/Price/{price}"
        )]
    void SetPriceDataForZipCode(string zipCode, string price);
}

Got this from here:
http://stackoverflow.com/questions/385451/wcf-restful-post-question

Cheers,

Darren
0
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

 
LVL 3

Assisted Solution

by:shekhar_shashi
shekhar_shashi earned 0 total points
ID: 35183189
Hi Darren,

I managed to fix the issue by doing the following:

1. Removed old code and started from scratch.
2. Created WCF Library and added a new service and the same method.
The method signature is:

[WebInvoke(Method = "POST", UriTemplate = "submitPayment", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
        [OperationContract]
        Stream submitPaymentToBank(Stream info);

3. Made sure I exposed an endpoint with WebHttpBinding binding. Also I added an endpoint behavior that used webHttp behavior.

Config file is:

<system.serviceModel>
    <services>
      <service behaviorConfiguration="CreditCardWebServiceREST.CreditCardWebServiceRESTBehavior"
        name="CreditCardWebServiceREST.CreditCardWebServiceREST">
        <clear />
        <endpoint behaviorConfiguration="WebBehavior" binding="webHttpBinding"
          bindingConfiguration="" contract="CreditCardWebServiceREST.ICreditCardWebServiceREST"
          listenUriMode="Explicit" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"
          listenUriMode="Explicit" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:2524/CreditCardWebServiceREST/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="CreditCardWebServiceREST.CreditCardWebServiceRESTBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
 
4. I added a new WCF website that uses the WCF library. This is a pre-compiled site that has a service file with this code.

<%@ ServiceHost Service="CatalogWebServiceREST.CatalogWebServiceREST"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

Also - both projects have a reference to System.ServiceModel.Web.


5. This is my client code.


  string baseAddress = "http://localhost:xxxx/CreditCardService.svc";

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(baseAddress + "/submitPayment/");
            req.Method = "POST";
            req.ContentType = "text/plain";

            Stream reqStream = req.GetRequestStream();
            string inputData = "MyData";
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            byte[] DataToSend = encoding.GetBytes(inputData);
            reqStream.Write(DataToSend, 0, DataToSend.Length);
            reqStream.Close();
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Console.WriteLine("Client: Receive Response HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);

            StreamReader loResponseStream = new StreamReader(resp.GetResponseStream(), encoding);
            string lcHtml = loResponseStream.ReadToEnd();
            Console.WriteLine(lcHtml);
            Console.ReadLine();


This code is stolen from:
http://www.west-wind.com/presentations/dotnetwebrequest/dotnetwebrequest.htm

Everything works like a charm!!!


0
 
LVL 3

Author Comment

by:shekhar_shashi
ID: 35183204
Hi Darren,

Thanks for the all the research work. You deserve full points and so I'll award points shortly.

0
 
LVL 3

Author Closing Comment

by:shekhar_shashi
ID: 35221232
Thanks
0

Featured Post

Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

Question has a verified solution.

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

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
SHARE your personal details only on a NEED to basis. Take CHARGE and SECURE your IDENTITY. How do I then PROTECT myself and stay in charge of my own Personal details (and) - MY own WAY...
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

17 Experts available now in Live!

Get 1:1 Help Now