Recently, a project has been made that requires the XML string of the generated SOAP request to be submitted to the server address, and then the returned SOAP response is parsed to determine the result. This involves attaching a certificate to the request and verifying it.
In fact, this function is not very difficult to achieve, but in the process of research has a few small problems but let me more than a few brain cells, in this record, for themselves and Bo friends do a little reminder.
The main problems that arise are the following points:
- Reported WebException exception: The request was aborted:could not create SSL/TLS secure channel.
- Internal 500 error reported on server
These two problems are mainly in the code which parts will appear, go to the code to see.
OK, first paragraph of the Code
1 /// <summary>2 ///SOAP requests made by the server for SSL authentication3 /// </summary>4 /// <param name= "Tokenid" >parameter 1 required to generate a SOAP request</param>5 /// <param name= "SecureCode" >parameter 2 required to generate a SOAP request</param>6 /// <returns>response content for SOAP requests</returns>7 Public stringSoaprequest (stringTokenid,stringSecureCode)8 {9 stringURL ="https://[the request URL for your SOAP request]";Ten stringSOAPAction ="[the action name]"; One A //The Generatesoapcontent method is used to construct the XML request for SOAP - stringSoaprequest =generatesoapcontent (Tokenid, secureCode); - the //This sentence is used to invoke the Validatefbcertificate method to make the remote SSL certificate validation successful -Servicepointmanager.servercertificatevalidationcallback =NewRemotecertificatevalidationcallback (validatefbcertificate); - - //set the security protocol type, which needs to correspond to the specific requirements +Servicepointmanager.securityprotocol =SECURITYPROTOCOLTYPE.SSL3; - +HttpWebRequest WebRequest =(HttpWebRequest) webrequest.create (URL); AWEBREQUEST.HEADERS.ADD ("SOAPAction", soapaction); atWebrequest.contentlength =soaprequest.length; -Webrequest.method ="POST"; -Webrequest.protocolversion =Httpversion.version10; - - //to join a certificate in a Web request -X509Certificate2 Cert2 =NewX509Certificate2 (Server.MapPath ("file name in PFX or P12 format"),"Certificate Password", X509keystorageflags.machinekeyset |x509keystorageflags.persistkeyset); in WebRequest.ClientCertificates.Add (CERT2); - to using(Stream Requeststream =Webrequest.getrequeststream ()) + { - byte[] Parambytes =Encoding.UTF8.GetBytes (soaprequest); therequestStream.Write (Parambytes,0, parambytes.length); * requeststream.close (); $ }Panax Notoginseng -WebResponse WebResponse =NULL; the Try + { AWebResponse =webrequest.getresponse (); the using(StreamReader myreader =NewStreamReader (WebResponse.GetResponseStream (), Encoding.UTF8)) + { - stringResponse =myreader.readtoend (); $ myreader.close (); $ returnresponse; - } - } the Catch(Exception ex) - {Wuyi Throwex; the } - } Wu - /// <summary> About ///Generate the SOAP request $ /// </summary> - /// <param name= "UserId" >The token Id</param> - /// <param name= "SecureCode" >The Secure Code</param> - /// <returns>The request string</returns> A stringGeneratesoapcontent (stringUseridstringSecureCode) + { the return "<?xml version=\ "1.0\" encoding=\ "utf-8\"? ><soapenv:envelope xmlns:soapenv=\ "/http Schemas.xmlsoap.org/soap/envelope/\ "xmlns:vip=\" https://schemas.symantec.com/vip/2011/04/vipuserservices\ "> " -+"<soapenv:Header/>" $+"<soapenv:Body>" the+"<vip:AuthenticateUserRequest>" the+"<vip:requestId>1323</vip:requestId>" the+"<vip:userId>"+ UserId +"</vip:userId>" the+"<vip:otpAuthData>" -+"<vip:otp>"+ SecureCode +"</vip:otp>" in+"</vip:otpAuthData>" the+"</vip:AuthenticateUserRequest>" the+"</soapenv:Body>" About+"</soapenv:Envelope>"; the}
The above is the basic code that has solved the problem.
In code, when 16, 19 lines of code are not written, an exception may occur: "The request was aborted:could not the Create SSL/TLS secure channel." When 28, 29 lines are not written, that exception will definitely occur.
In the 500 internal error, I always thought it was the server side of the method error, until the SOAP request with PHP's curl to successfully return the results, only to find that a small problem caused this "serious" trouble. The reason for this is that the original Generatesoapcontent method is written as follows when writing the SOAP request text:
1 /// <summary>2 ///Generate the SOAP request3 /// </summary>4 /// <param name= "UserId" >The token Id</param>5 /// <param name= "SecureCode" >The Secure Code</param>6 /// <returns>The request string</returns>7 stringGeneratesoapcontent (stringUseridstringSecureCode)8 {9StringBuilder Builder =NewStringBuilder ();TenBuilder. Appendline ("<?xml version=\ "1.0\" encoding=\ "utf-8\"?>"); OneBuilder. Appendline ("<s:envelope xmlns:s=\ "http://schemas.xmlsoap.org/soap/envelope/\" >"); ABuilder. Appendline ("<S:Body>"); -Builder. Appendline ("<authenticateuserrequest xmlns=\ "https://schemas.vip.symantec.com\" >"); -Builder. Appendline ("<requestId>"+ ((Int32) (NewRandom (). Nextdouble () * +) +1) +"</requestId>"); theBuilder. Appendline ("<userId>"+ UserId +"</userId>"); -Builder. Appendline ("<pin></pin>"); -Builder. Appendline ("<otpAuthData>"); -Builder. Appendline ("<otp>"+ SecureCode +"</otp>"); +Builder. Appendline ("</otpAuthData>"); -Builder. Appendline ("</AuthenticateUserRequest>"); +Builder. Appendline ("</S:Body>"); ABuilder. Appendline ("</S:Envelope>"); at returnBuilder. ToString (); -}
See how it differs from the top method? Yes, StringBuilder's appendline me, because this will add the "\ r \ n" character to the resulting soap string, which will throw an exception if it is uploaded to the server side without being recognized by the server-side method as the XML format of soap.
In fact, these are not difficult to solve the problem, mainly because they do not pay attention. Leave this article here to provide some help to the later people.
Submit and get a response to a SOAP request with SSL authentication