socket模拟Http协议上传数据到服务器
- 提问者网友:富士山上尢
- 2021-04-24 01:38
- 五星知识达人网友:旧脸谱
- 2021-04-24 02:14
package ServerPackage;
import java.io.*;
import java.net.*;
public class ClientTwo {
private Socket sk=null;
private BufferedReader buf=null;
private BufferedReader tobuf=null;
private PrintWriter pw=null;
public ClientTwo(){
try {
sk=new Socket(InetAddress.getLocalHost(),4444);
System.out.println("客户连接成功!");
buf=new BufferedReader(new InputStreamReader(sk.getInputStream()));
tobuf=new BufferedReader(new InputStreamReader(System.in));
pw=new PrintWriter(sk.getOutputStream(),true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void Talk(){
try {
String stg=tobuf.readLine(); //获取从控制台输入的路径
pw.println(stg);
String content=buf.readLine();
while(content!=null){
System.out.println(content);
content=buf.readLine();
}
pw.close();
tobuf.close();
buf.close();
sk.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ClientTwo().Talk();
}
}
服务器端:
package ServerPackage;
import java.io.*;
import java.net.*;
public class ServerTwo {
private ServerSocket ss=null;
private Socket sk=null;
private BufferedReader buf=null;
private BufferedReader tobuf=null;
private PrintWriter pw=null;
public ServerTwo(){
try {
ss=new ServerSocket(4444);
System.out.println("服务器等待客户的连接....");
sk=ss.accept();
System.out.println("有客户已连接");
buf=new BufferedReader(new InputStreamReader(sk.getInputStream()));
pw=new PrintWriter(sk.getOutputStream(),true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void Send(){
try {
String stg=buf.readLine();
File fe=new File(stg);
if(fe.exists()){
System.out.println("开始发送.......");
tobuf=new BufferedReader(new FileReader(fe));
String str=tobuf.readLine();
while(str!=null){
pw.println(str);
str=tobuf.readLine();
}
System.out.println("发送完毕......");
tobuf.close();
pw.close();
sk.close();
ss.close();
}else{
pw.println("没有你想要的内容!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ServerTwo().Send();
}
}
你试试吧。。。。。。。
- 1楼网友:雪起风沙痕
- 2021-04-24 03:44
楼上你这是http协议吗?
我抄录一段别人的代码,希望对你有帮助:
private final void __sendRequest(String requestBody, Socket socket) throws IOException { StringBuffer url = new StringBuffer(); StringBuffer connection = new StringBuffer(); if (__proxy == null) { url.append(__server.get("path")); connection.append("Connection: "); connection.append(__keepAlive ? "Keep-Alive" : "close"); connection.append("\r\n"); connection.append("Pragma: no-cache\r\n"); connection.append("Cache-Control: no-cache\r\n"); } else { url.append(__server.get("scheme")); url.append("://"); url.append(__server.get("host")); url.append(":"); url.append(__server.get("port")); url.append(__server.get("path")); connection.append("Proxy-Connection: "); connection.append(__keepAlive ? "Keep-Alive" : "close"); connection.append("\r\n"); if (__proxy.get("userinfo") != null) { connection.append("Proxy-Authorization: Basic "); connection.append(Base64.encode(((String) __proxy.get("userinfo")).getBytes(__charset))); connection.append("\r\n"); } } StringBuffer auth = new StringBuffer(); if (__server.get("userinfo") != null) { auth.append("Authorization: Basic "); auth.append(Base64.encode(((String) __server.get("userinfo")).getBytes(__charset))); auth.append("\r\n"); } StringBuffer cookie = new StringBuffer(); if (__cookie != null) { cookie.append("Cookie: "); cookie.append(__cookie); cookie.append("\r\n"); } byte[] rb = requestBody.getBytes(); StringBuffer requestHeader = new StringBuffer(); requestHeader.append("POST ").append(url).append(" HTTP/1.1\r\n"); requestHeader.append("Host: ").append(__server.get("host")).append(':').append(__server.get("port")).append("\r\n"); requestHeader.append("User-Agent: PHPRPC Client 3.0 for Java\r\n"); requestHeader.append(auth).append(connection).append(cookie); requestHeader.append("Accept: */*\r\n"); requestHeader.append("Accept-Encoding: gzip,deflate\r\n"); requestHeader.append("Content-Type: application/x-www-form-urlencoded; charset=").append(__charset).append("\r\n"); requestHeader.append("Content-Length: ").append(rb.length).append("\r\n"); requestHeader.append("\r\n"); __sendRequest(requestHeader.toString().getBytes(), rb, socket); }
private final void __sendRequest(byte[] header, byte[] body, Socket socket) throws IOException { BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream()); os.write(header); os.write(body); os.flush(); }