Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中删除socket通信中的头信息_Java_Sockets - Fatal编程技术网

如何在java中删除socket通信中的头信息

如何在java中删除socket通信中的头信息,java,sockets,Java,Sockets,我不熟悉套接字编程。 我已从客户端程序连接到服务器,但得到的响应是 标题信息+实际内容(我需要的ie XML数据) 我只想删除标题。 这是我的代码: 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(); Stri

我不熟悉套接字编程。 我已从客户端程序连接到服务器,但得到的响应是
标题信息+实际内容(我需要的ie XML数据)
我只想删除标题。
这是我的代码:

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());    
}

}
}
公共类TestSocket{
公共静态void main(字符串参数[]){
试一试{
URL=新URL(“http://xxxx.de:8080/abcd");
字符串路径=url.getFile();
int-port=url.getPort();
String host=url.getHost();
Socket cliSocket=新套接字(主机、端口);
字符串req=“yyy”;
req=“name=”+req;
BufferedWriter bw=新的BufferedWriter(新的OutputStreamWriter(cliSocket.getOutputStream());
write(“POST”+path+“HTTP/1.0\r\n”);
写入(“主机:“+Host+”\r\n”);
write(“内容长度:“+req.Length()+”\r\n”);
write(“内容类型:application/x-www-form-urlencoded\r\n”);
写入(“\r\n”);
写入(req);
bw.flush();
BufferedReader rd=新的BufferedReader(新的InputStreamReader(cliSocket.getInputStream());
弦线;
System.out.println(“步骤4:获取输入流”);
StringBuffer serverData=新的StringBuffer(“”);
而((line=rd.readLine())!=null){
serverData.append(行);
}
System.out.println(服务器数据);
String data=serverData.toString();

int index=data.indexOf("使用类似于表单Apache HttpComponents的功能。这样可以避免大部分HTTP内容,并让您直接处理消息内容。

您可以同时发布服务器数据吗?我认为您必须手动操作数据,并从代码中的响应中删除头信息。是否要删除xml头?@Octopus请看回复,我已经编辑了question@latifmohammadkhanKenneth的链接将带您找到答案,接受其中一个。@Octopus我已经编写了该代码,在我的程序中,我需要任何其他方式,即不是通过编程逻辑。我需要任何预定义的技术来分离标题和内容