import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class StringMethord {
/**
* @param args
*/
public static void main(String[] args) {
String xml= processRequest("SZX","111","city","SZV");
// System.out.println(xml);
String reslut=getConnection("http://gbe.jin.cn/Entrance/InnerRequestListener.aspx",xml );
System.out.println(reslut);
}
public static String getConnection(String url,String content)
{
PrintWriter out = null;
InputStream input = null;
StringBuffer retCode=new StringBuffer() ;
try {
int b;
URL server = null;
server = new URL(url);
HttpURLConnection connection = (HttpURLConnection) server
.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "text/xml");
connection.setDoOutput(true);
out = new PrintWriter(connection.getOutputStream());
out.print(content);
out.close();
out = null;
input = connection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(input));
String temp;
while((temp=bf.readLine())!=null)
{
retCode.append(temp);
}
// int len = input.available();
// byte[] buff = new byte[1024*4];
//
// while ((b = input.read(buff)) != -1);
// {
// String temp = new String(buff);
// System.out.println(temp);
// retCode.append(temp);
// }
} catch (IOException e) {
e.printStackTrace();
} finally {
closeUrl(input, out);
}
return retCode.toString();
}
private static void closeUrl(InputStream input, Writer output) {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String processRequest(String userName,String passWord,String serchType,String serchTypeValue)
{
StringBuffer xmlStream =new StringBuffer("");
xmlStream.append("<?xml version=/"1.0/" encoding=/"GB2312/"?>");
xmlStream.append("<Request>");
xmlStream.append("<Source>");
xmlStream.append("<RequestorID OfficeID=/"").append(userName).append("/" ").append("Passport=/"").append(passWord).append("/"/>");
xmlStream.append("<RequestorPreferences Language=/"zh/">");
xmlStream.append("<RequestMode>SYNCHRONOUS</RequestMode>");
xmlStream.append("</RequestorPreferences></Source><RequestDetails>");
xmlStream.append("<SearchItemRequest ItemType=/"hotel/">");
xmlStream.append("<ItemDestination DestinationType=/"").append(serchType).append("/" ").append("DestinationCode=/"").append(serchTypeValue).append("/"/>");
xmlStream.append("</SearchItemRequest></RequestDetails></Request>");
return xmlStream.toString();
}
public static void analysisContent(String content)
{
StringReader sr = new StringReader(content);
try {
SAXReader reader = new SAXReader();
Document doc = reader.read(sr);
List<Node> list = doc.selectNodes("//ItemDetail");
for (Node node : list)
{
String city = node.valueOf("City");
String location = node.valueOf("Item");
String cityCode=((Element) node).element("City").attribute("Code").getText();
String locationCode=((Element) node).element("Item").attribute("Code").getText();
System.out.println(city+" "+cityCode+"~~"+location+" "+locationCode);
}
}catch (Exception e)
{
e.printStackTrace();
}
}
}
//<?xml version="1.0" encoding="GB2312"?>
//<Request>
// <Source>
// <RequestorID OfficeID="SHA118" Passport="111111" />
// <RequestorPreferences Language="zh">
// <RequestMode>SYNCHRONOUS</RequestMode>
// </RequestorPreferences>
// </Source>
// <RequestDetails>
// <SearchItemRequest ItemType="hotel">
// <ItemDestination DestinationType="city" DestinationCode="SZV" />
// </SearchItemRequest>
// </RequestDetails>
//</Request>
//<?xml version="1.0" encoding="GB2312" ?>
//<Response ResponseReference="54667ad4-6d3b-4d5b-97c9-bb0a07c625d6">
// <ResponseDetails>
// <SearchItemResponse ItemType="hotel">
// <ItemDetails>
// <ItemDetail>
// <City Code="SZV"><![CDATA[苏州]]></City>
// <Item Code="HC100697"><![CDATA[汉庭快捷酒店(苏州桐泾北路店)]]></Item>
// </ItemDetail>
// <ItemDetail>
// <City Code="SZV"><![CDATA[苏州]]></City>
// <Item Code="HC100730"><![CDATA[如家快捷酒店(苏州木渎店)]]></Item>
// </ItemDetail>
// </ItemDetails>
// </SearchItemResponse>
// </ResponseDetails>
//</Response>
<?xml version="1.0" encoding="GB2312"?>
<Request>
<Source>
<RequestorID OfficeID="SZX106" Passport="102569" />
<RequestorPreferences Language="zh">
<RequestMode>SYNCHRONOUS</RequestMode>
</RequestorPreferences>
</Source>
<RequestDetails>
<SearchItemInformationRequest ItemType="hotel"><ItemDestination DestinationType="city" DestinationCode="SZX" />
<ItemCode>1E7IX</ItemCode>
</SearchItemInformationRequest>
</RequestDetails>
</Request>