我是新的套接字编程。我已经从我的客户端程序连接到服务器,但是我得到的响应是
Header info实际内容(我需要的是XML数据)
我只想删除Headers。
这是我的代码:
public class TestSocket{
public static void main(String args[]){
try{
URL url = new URL("http://xxxx.de:8080/abcd");
String path=url.getFile();
int port = url.getPort();
String host = url.getHost();
Socket cliSocket = new Socket(host,port);
String req = "yyyy";
req="name="+req;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(cliSocket.getOutputStream()));
bw.write("POST " + path + " HTTP/1.0\r\n");
bw.write("Host: " + host + "\r\n");
bw.write("Content-Length: " + req.length() + "\r\n");
bw.write("Content-Type: application/x-www-form-urlencoded\r\n");
bw.write("\r\n");
bw.write(req);
bw.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(cliSocket.getInputStream()));
String line;
System.out.println("Step 4 : Getting Input Stream");
StringBuffer serverData = new StringBuffer("");
while ((line = rd.readLine()) != null) {
serverData.append(line);
}
System.out.println(serverData);
String data = serverData.toString();
int index = data.indexOf("<");
String xmlData =null;
if(index!=-1){
xmlData = data.substring(index);
System.out.println("XML Content :"+xmlData);
}else{
System.out.println("XML Data Not Retrived");
}
bw.close();
rd.close();
}catch(java.net.UnknownHostException uh){
System.out.println("UH : Host Not Found ");
}catch(IOException ioe){
System.out.println("IO Exp "+ioe.getMessage());
}catch(Exception e){
System.out.println("Exp "+e.getMessage());
}
}
}
响应:
HTTP/1.1 200 OKServer: Apache-Coyote/1.1X-Powered-By:Servlet 2.4;JBoss-4.2.3.GA(build:SVNTag=JBoss_4_2_3_GAdate=200807181417)/JBossWeb-2.0Content-Type:text/xml;charset=UTF-8Content-Llong:1110 Date:Wed,30 Apr 2014 12:13:10 GMT Connection:关闭
然后XML数据,
我只需要XML数据,不HTTP/1.1 200 OKServer: Apache-Coyote/1.1X-Powered-By:Servlet 2.4;JBoss-4.2.3.GA…………等等
使用类似于Apache HttpComponents的HttpClient表单。这将使您免于大多数HTTP的东西,并允许您直接处理消息内容。