Bonjour,
J'essai de mettre en place un webservice, hors je rencontre des difficultés pour communiquer avec en JSON, si je fais une web_references dans un projet en VB (winform/web) cela fonctionne, mais pas avec json.
Merci de votre aide
http://mydomain1.com/Service.svc
http://mydomain1.com/Service.svc?wsdl
Webservice
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 This XML file does not appear to have any style information associated with it. The document tree is shown below. <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" name="Service" targetNamespace="http://tempuri.org/"> <wsdl:types> <xsd:schema targetNamespace="http://tempuri.org/Imports"> <xsd:import schemaLocation="http://mydomain1.com/Service.svc?xsd=xsd0" namespace="http://tempuri.org/"/> <xsd:import schemaLocation="http://mydomain1.com/Service.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/> <xsd:import schemaLocation="http://mydomain1.com/Service.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/"/> </xsd:schema> </wsdl:types> <wsdl:message name="IService_GetData_InputMessage"> <wsdl:part name="parameters" element="tns:GetData"/> </wsdl:message> <wsdl:message name="IService_GetData_OutputMessage"> <wsdl:part name="parameters" element="tns:GetDataResponse"/> </wsdl:message> <wsdl:message name="IService_GetDataUsingDataContract_InputMessage"> <wsdl:part name="parameters" element="tns:GetDataUsingDataContract"/> </wsdl:message> <wsdl:message name="IService_GetDataUsingDataContract_OutputMessage"> <wsdl:part name="parameters" element="tns:GetDataUsingDataContractResponse"/> </wsdl:message> <wsdl:message name="IService_AddToNumber_InputMessage"> <wsdl:part name="parameters" element="tns:AddToNumber"/> </wsdl:message> <wsdl:message name="IService_AddToNumber_OutputMessage"> <wsdl:part name="parameters" element="tns:AddToNumberResponse"/> </wsdl:message> <wsdl:message name="IService_GetJsonTest1_InputMessage"> <wsdl:part name="parameters" element="tns:GetJsonTest1"/> </wsdl:message> <wsdl:message name="IService_GetJsonTest1_OutputMessage"> <wsdl:part name="parameters" element="tns:GetJsonTest1Response"/> </wsdl:message> <wsdl:message name="IService_GetJsonTest2_InputMessage"> <wsdl:part name="parameters" element="tns:GetJsonTest2"/> </wsdl:message> <wsdl:message name="IService_GetJsonTest2_OutputMessage"> <wsdl:part name="parameters" element="tns:GetJsonTest2Response"/> </wsdl:message> <wsdl:portType name="IService"> <wsdl:operation name="GetData"> <wsdl:input wsaw:Action="http://tempuri.org/IService/GetData" message="tns:IService_GetData_InputMessage"/> <wsdl:output wsaw:Action="http://tempuri.org/IService/GetDataResponse" message="tns:IService_GetData_OutputMessage"/> </wsdl:operation> <wsdl:operation name="GetDataUsingDataContract"> <wsdl:input wsaw:Action="http://tempuri.org/IService/GetDataUsingDataContract" message="tns:IService_GetDataUsingDataContract_InputMessage"/> <wsdl:output wsaw:Action="http://tempuri.org/IService/GetDataUsingDataContractResponse" message="tns:IService_GetDataUsingDataContract_OutputMessage"/> </wsdl:operation> <wsdl:operation name="AddToNumber"> <wsdl:input wsaw:Action="http://tempuri.org/IService/AddToNumber" message="tns:IService_AddToNumber_InputMessage"/> <wsdl:output wsaw:Action="http://tempuri.org/IService/AddToNumberResponse" message="tns:IService_AddToNumber_OutputMessage"/> </wsdl:operation> <wsdl:operation name="GetJsonTest1"> <wsdl:input wsaw:Action="http://tempuri.org/IService/GetJsonTest1" message="tns:IService_GetJsonTest1_InputMessage"/> <wsdl:output wsaw:Action="http://tempuri.org/IService/GetJsonTest1Response" message="tns:IService_GetJsonTest1_OutputMessage"/> </wsdl:operation> <wsdl:operation name="GetJsonTest2"> <wsdl:input wsaw:Action="http://tempuri.org/IService/GetJsonTest2" message="tns:IService_GetJsonTest2_InputMessage"/> <wsdl:output wsaw:Action="http://tempuri.org/IService/GetJsonTest2Response" message="tns:IService_GetJsonTest2_OutputMessage"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="BasicHttpBinding_IService" type="tns:IService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="GetData"> <soap:operation soapAction="http://tempuri.org/IService/GetData" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="GetDataUsingDataContract"> <soap:operation soapAction="http://tempuri.org/IService/GetDataUsingDataContract" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="AddToNumber"> <soap:operation soapAction="http://tempuri.org/IService/AddToNumber" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="GetJsonTest1"> <soap:operation soapAction="http://tempuri.org/IService/GetJsonTest1" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="GetJsonTest2"> <soap:operation soapAction="http://tempuri.org/IService/GetJsonTest2" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="Service"> <wsdl:port name="BasicHttpBinding_IService" binding="tns:BasicHttpBinding_IService"> <soap:address location="http://mydomain1.com/Service.svc"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
IService.vb
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 <ServiceContract()> Public Interface IService <OperationContract()> Function GetData(ByVal value As Integer) As String <OperationContract()> Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType ' TODO: ajoutez vos opérations de service ici <OperationContract()> Function AddToNumber(num1 As Integer, num2 As Integer) As Integer <OperationContract()> Function GetJsonTest1(value As String) As String <OperationContract()> <WebInvoke(UriTemplate:="/GetJsonTest2/{value}", Method:="POST", ResponseFormat:=WebMessageFormat.Json)> Function GetJsonTest2(value As String) As String End Interface <DataContract()> Public Class CompositeType <DataMember()> Public Property BoolValue() As Boolean <DataMember()> Public Property StringValue() As String End Class
Service.vb
web.config
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 Imports System.Web.Script.Serialization Public Class Service Implements IService Public Sub New() End Sub Public Function GetData(ByVal value As Integer) As String Implements IService.GetData Return String.Format("You entered: {0}", value) End Function Public Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType Implements IService.GetDataUsingDataContract If composite Is Nothing Then Throw New ArgumentNullException("composite") End If If composite.BoolValue Then composite.StringValue &= "Suffix" End If Return composite End Function Public Function AddToNumber(num1 As Integer, num2 As Integer) As Integer Implements IService.AddToNumber Dim output As Integer = 0 output = num1 + num2 Return output End Function Public Function GetJsonTest1(value As String) As String Implements IService.GetJsonTest1 If Trim(value & "") = "" Then value = "No Value" End If Dim output As String = "[{" & Chr(34) & "return" & Chr(34) & ": " & Chr(34) & value & Chr(34) & "}]" Return output End Function Public Function GetJsonTest2(value As String) As String Implements IService.GetJsonTest2 Dim output As New List(Of Object)() output.Add(New With {Key .Id = "key", Key .Name = value}) Return (New JavaScriptSerializer().Serialize(output)) End Function End Class
http://mydomain2.com/test.html
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <customErrors mode="Off"/> <compilation debug="false" strict="false" explicit="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> <pages> <namespaces> <add namespace="System.Runtime.Serialization" /> <add namespace="System.ServiceModel" /> <add namespace="System.ServiceModel.Web" /> </namespaces> </pages> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- Pour éviter la divulgation d'informations de métadonnées, définissez les valeurs ci-dessous sur false avant le déploiement --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- Pour recevoir les détails de l'exception dans les erreurs à des fins de débogage, définissez la valeur ci-dessous sur true. Pour éviter la divulgation d'informations d'exception, définissez-la sur false avant le déploiement --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!-- Pour parcourir le répertoire racine de l'application Web lors du débogage, définissez la valeur ci-dessous sur true. Définissez-la sur false avant le déploiement pour ne pas divulguer d'informations du dossier de l'application Web. --> <directoryBrowse enabled="true"/> <handlers> <add name="svc-integrated" path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler" resourceType="File" preCondition="integratedMode" /> </handlers> </system.webServer> </configuration>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>myApp</title> <script src="/js/jquery-3.2.1.min.js"></script> </head> <body> <div id="jQueryRslt"></div> <script> $(document).ready(function () { WCFJSON(); }); function WCFJSON() { var myVar = "jquerie-value"; Type = "POST"; Url = "http://mydomain1.com/Service.svc/GetJsonTest2"; //UrlResponse = "http://mydomain1.com/Service.svc/GetJsonTest2Response"; Data = JSON.stringify(myVar); ContentType = "application/json; charset=utf-8"; DataType = "json"; VarProcessData = false; CallService(); } function CallService() { $.ajax({ type: Type, url: Url, data: Data, contentType: ContentType, //headers: {SOAPAction: UrlResponse}, dataType: DataType, processdata: VarProcessData, success: function (result) { ServiceSucceeded(result); }, error : ServiceFailed }); } function ServiceFailed(xhr) { var debug = 'Service call failed: ' + xhr.status; if (xhr.responseText) { var err = xhr.responseText; if (err) debug += err; else debug += ' Message: "Unknown server error."'; } jQueryRslt.innerHTML = debug; } function ServiceSucceeded(result) { jQueryRslt.innerHTML = result; } </script> </body> </html>
Erreur JSON
Partager