การเขียนโปรแกรมเพื่อตรวจสอบหมายเลขบัตรประจำตัวประชาชนโดยเรียกใช้บริการเว็บเซอร์วิสกรมสรรพากร


web services ssl client rdws https เว็บเซอร์วิส กรมสรรพากร

หลายโปรแกรมอาจต้องการมีส่วนในการตรวจสอบความถูกต้องของหมายเลขประจำตัวป ระชาชน ซึ่งทางกรมสรรพากรก็ให้บริการเว็บเซอร์วิสนี้ เนื่องจากเว็บเซอร์วิสของกรมสรรพกรให้บริการโดยใช้ SSL เพื่อช่วยทำให้มีความปลอดภัยมากยิ่งขึ้น ดังนั้นต้องเข้าโดย “HTTPS” แทนที่จะเป็น “HTTP” ปกติ

ข้างล่างนี้โค้ดที่ใช้ในการเรียกใช้เว็บเซอร์วิสและโอเปอเรชันดังกล่าว และใช้โปรแกรม XTrustProvider.java ที่ SSL Trust Provider for Java เพื่อใช้ในการเรียกเว็บเซอร์วิสที่เข้าถึงโดย HTTPS

package callsoapws;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Source;
import javax.xml.soap.MessageFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.MimeHeaders;
import com.sun.net.ssl.internal.ssl.*;
import java.security.Security;

/**
* @author Kanda Runapongsa Saikaew and Pongsakorn Poosankam
* Computer Engineering Department
* Khon Kaen University
*
*/
public class CheckPinRDWS {
public void msgEnvelope(String[] args) throws Exception {

SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnectionFactory.createConnection();
MessageFactory messageFactory =
MessageFactory.newInstance();

// Create a message
SOAPMessage message =
messageFactory.createMessage();

// Get the SOAP header and body from the message
// and remove the header
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
header.detachNode();

// Create a SOAP factory
// Create a UDDI v2 checkPin body element
SOAPFactory soapFactory =
SOAPFactory.newInstance();
SOAPBodyElement checkPin =
body.addBodyElement(soapFactory.createName("ServicePIN",
"ns", "https://rdws.rd.go.th/ServiceRD/CheckTINPINService"));

SOAPElement username =
checkPin.addChildElement(
soapFactory.createName("username","ns",
"https://rdws.rd.go.th/ServiceRD/CheckTINPINService"));
username.addTextNode("anonymous");

SOAPElement password =
checkPin.addChildElement(
soapFactory.createName("password","ns",
"https://rdws.rd.go.th/ServiceRD/CheckTINPINService"));
password.addTextNode("anonymous");

SOAPElement pin =
checkPin.addChildElement(
soapFactory.createName("PIN","ns",
"https://rdws.rd.go.th/ServiceRD/CheckTINPINService"));
pin.addTextNode("xxx TEST PIN NUMBER xxx");

MimeHeaders hd = message.getMimeHeaders();
hd.addHeader("SOAPAction",
"https://rdws.rd.go.th/ServiceRD/CheckTINPINService/ServicePIN");

message.saveChanges();
System.out.println("REQUEST:");
//Display Request Message
displayMessage(message);

System.out.println("\n\n");
//add code below for trust x.509 ceritficate
XTrustProvider.install();
SOAPConnection conn =
SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = conn.call(message,
"https://rdws.rd.go.th/ServiceRD/CheckTINPINService.asmx");

System.out.println("RESPONSE:");
//Display Response Message
displayMessage(response);
}

public void displayMessage(SOAPMessage message) throws Exception {
TransformerFactory tFact = TransformerFactory.newInstance();
Transformer transformer = tFact.newTransformer();
Source src = message.getSOAPPart().getContent();
StreamResult result = new StreamResult( System.out );
transformer.setOutputProperty(OutputKeys.ENCODING, "tis-620");
transformer.transform(src, result);
}
public static void main(String[] args) throws Exception {
CheckPinRDWS clientApp = new CheckPinRDWS();
clientApp.msgEnvelope(args);
}
}

ผลลัพธ์ีที่ได้เมื่อหมายเลขบัตรประจำตัวประชาชนถูกต้อง

ผลลัพธที่ได้เมื่อหมายเลขบัตรประจำตัวประชาชนไม่ถูกต้อง

คำสำคัญ (Tags): #web services#client#ssl#https
หมายเลขบันทึก: 139462เขียนเมื่อ 17 ตุลาคม 2007 16:33 น. ()แก้ไขเมื่อ 11 กุมภาพันธ์ 2012 21:04 น. ()สัญญาอนุญาต: จำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (1)
พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท