我正在开发一个用于在线投票的应用程序,其中登录过程是通过 dotnet 中的 web 服务进行的。网络服务部分已准备就绪并且可以工作,但我想看看返回了什么响应。
URL url=new URL("http://www.example.com/login");
HttpURLConnection urlconnection=(HttpURLConnection)url.openConnection();
String requestXML="<data><username>sachin.t@gmail.com</username><password>sachin</password></data>";
urlconnection.setRequestMethod("POST");
urlconnection.setDoInput(true);
urlconnection.setDoOutput(true);
urlconnection.setRequestProperty("Accept-Charset", "UTF-8");
urlconnection.setRequestProperty("Content-type", "application/xml");
urlconnection.setRequestProperty("Content-Length", ""+Integer.toString(requestXML.length()));
//Send request
DataOutputStream dout=new DataOutputStream(urlconnection.getOutputStream());
dout.writeBytes(requestXML);
dout.flush();
dout.close();
if(urlconnection.getResponseCode()==HttpURLConnection.HTTP_OK)
{
InputStream is=urlconnection.getInputStream();
BufferedReader breader=new BufferedReader(new InputStreamReader(is));
String temp;
String resp="";
while((temp=breader.readLine())!=null)
{
resp+=temp;
}
Document respdoc=null;
DocumentBuilderFactory docbuilderfactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder=docbuilderfactory.newDocumentBuilder();
InputSource isource=new InputSource();
isource.setCharacterStream(new StringReader(resp));
respdoc=docbuilder.parse(isource);
从网上我得到了这个,但我想解析下面给出的响应。
<login>
<login_validation>"1/2/3"</login_validation>
( 1-invalid username,2-successfull and 3- already voted)
<voter_id>" 0/voter_id "</voter_id>
(if sucessfull then only voter_id else value will return 0)
</login>
有什么建议吗?
先感谢您。