using System.Runtime.Serialization; namespace TestService { [DataContract] public class Message { [DataMember(Order = 1)] public string Header { get; set; } [DataMember(Order =2)] public string Body { get; set; } } }
using System.ServiceModel; using System.ServiceModel.Web; namespace TestService { [ServiceContract] public interface ITestService { [OperationContract] [WebGet(UriTemplate = "/GetMessage/?header={header}&?body={body}", ResponseFormat = WebMessageFormat.Json)] Message ComposeMessage(string header, string body); } }
using System.ServiceModel.Activation; namespace TestService { public class TestService : ITestService { #region ITestService Members public Message ComposeMessage(string header, string body) { Message message = new Message() { Header = header, Body = body }; return message; } #endregion } }
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="TestService.TestService"> <endpoint binding="webHttpBinding" contract="TestService.ITestService" behaviorConfiguration="webHttp"/> </service> </services> <behaviors> <endpointBehaviors> <behavior name="webHttp"> <webHttp helpEnabled="true"/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
When hosted inside WCF-managed IIS, the WCF environment typically requires an .svc file indicating the type of service, as well as entries in the web.config file informing the WCF of the endpoint (binding and behaviors among other settings).
<%@ ServiceHost Language="C#" Debug="true" Service="TestService.TestService" CodeBehind="TestService.svc.cs" %>
localhost / TestService / TestService.svc / GetMessage /? header = Hello &? body = WorldThe result should look like this:
{"Header": "Hello", "Body": "World"}This is JSON Response.
private String getMessage(String header, String body) { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(_resources.getString(R.string.serviceAddress) + "/GetMessage/?header="+ header + "&?body=" + body); HttpResponse response; try { response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String result = convertStreamToString(instream); JSONObject json = new JSONObject(result); // Parsing JSONArray nameArray = json.names(); JSONArray valArray = json.toJSONArray(nameArray); _getResponse = nameArray.getString(0) + "=" + valArray.getString(0) + ", " + nameArray.getString(1) + "=" + valArray.getString(1); instream.close(); } } catch (Exception e) { _getResponse = e.getMessage(); } return _getResponse; }
10.0.2.2/TestService/TestService.svcsince, when accessing localhost , we get Connection Refused IOException (who is interested, read more here ).
<uses-permission android:name="android.permission.INTERNET" />
Source: https://habr.com/ru/post/115157/
All Articles