如何使用webservice来校验一个邮箱的有效性:
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class ValiEmail {
public static boolean checkEmailByWebservice(String email) {
String soapRequestData = “”
+ “<?xml version=/”1.0/” encoding=/”utf-8/”?>”
+ “<soap:Envelope xmlns:xsi=/”http://www.w3.org/2001/XMLSchema-instance/” xmlns:xsd=/”http://www.w3.org/2001/XMLSchema/” xmlns:soap=/”http://www.w3.org/2003/05/soap-envelope/”>”
+ ” <soap:Body>”
+ ” <IsValidEmail xmlns=/”http://www.webservicex.net/”>”
+ ” <Email>” + email + “</Email>” + ” </IsValidEmail>”
+ ” </soap:Body>” + “</soap:Envelope>”;
try {
URL u = new URL(
“http://www.webservicex.net/ValidateEmail.asmx?op=IsValidEmail”);
URLConnection uc = u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty(“Content-Type”,”application/soap+xml; charset=utf-8″);
PrintWriter pw = new PrintWriter(uc.getOutputStream());
pw.println(soapRequestData);
pw.close();
DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = bf.newDocumentBuilder();
Document document = db.parse(uc.getInputStream());
String res = document.getElementsByTagName(“IsValidEmailResult”).item(0).getTextContent();
return Boolean.parseBoolean(res);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
boolean flag=checkEmailByWebservice(”
[email protected]“);
System.out.println(flag);
}
}
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/14712.html