HoneyDipper HoneyDipper - 3 months ago 16
JSON Question

WCF REstful Service Passing user and password on Uri

I do have a WCF RESTful .NET 4.0 service that runs and consumes values #Json from the body of the URI.

the way I invoke my method is the following:

Using Poster, as POST request
http://localhost:25512/JSONService1.svc/CreateItem

Body:

= YOLO

works fine!

but I want to consume the uri as well ,

http://localhost:25512/JSONService1.svc/CreateItem?uid=aaa&pass=111

here is my code

this is on Service1.cs

[OperationContract]
[WebInvoke(UriTemplate = "CreateItem", // ISSUES <<< not sure what to do here
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, Method = "POST")]

void CreateItem(Stream streamOfData,string uid, string pass);


and this the associated code with it , Service1.svc.cs

public void CreateItem(Stream streamOfData, string uid, string pass)
{
StreamReader reader = new StreamReader(streamOfData);
String res = reader.ReadToEnd();
NameValueCollection coll = HttpUtility.ParseQueryString(res);
}

Answer

To get the original URI you can do the following:

string originalUri = System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString;

You can use the System.Uri class and / or the HttpUtility class to parse the Uri.

To use the System.Uri class just pass in the Uri.

Uri myUri = new Uri("http://localhost:25512/JSONService1.svc/CreateItem?uid=aaa&pass=111");

or in your requirement:

Uri myUri = new Uri(originalUri);

Then you can examine various pieces of the Uri by using some of the built in properties.

Comments