「java发送请求」java发送请求模拟ip
本篇文章给大家谈谈java发送请求,以及java发送请求模拟ip对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java发送put请求
1.服务端
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
public class MultiThreadServer {
private int port=8821;
private ServerSocket serverSocket;
private ExecutorService executorService;//线程池
private final int POOL_SIZE=10;//单个CPU线程池大小
public MultiThreadServer() throws IOException{
serverSocket=new ServerSocket(port);
//Runtime的availableProcessor()方法返回当前系统的CPU数目.
executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
System.out.println("服务器启动");
}
public void service(){
while(true){
Socket socket=null;
try {
//接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
socket=serverSocket.accept();
executorService.execute(new Handler(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new MultiThreadServer().service();
}
}
class Handler implements Runnable{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}
private PrintWriter getWriter(Socket socket) throws IOException{
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket) throws IOException{
InputStream socketIn=socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public String echo(String msg){
return "echo:"+msg;
}
public void run(){
try {
System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
BufferedReader br=getReader(socket);
PrintWriter pw=getWriter(socket);
String msg=null;
while((msg=br.readLine())!=null){
System.out.println(msg);
pw.println(echo(msg));
if(msg.equals("bye"))
break;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.客户端
package sterning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultiThreadClient {
public static void main(String[] args) {
int numTasks = 10;
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i numTasks; i++) {
exec.execute(createTask(i));
}
}
// 定义一个简单的任务
private static Runnable createTask(final int taskID) {
return new Runnable() {
private Socket socket = null;
private int port=8821;
public void run() {
System.out.println("Task " + taskID + ":start");
try {
socket = new Socket("localhost", port);
// 发送关闭命令
OutputStream socketOut = socket.getOutputStream();
socketOut.write("shutdown\r\n".getBytes());
// 接收服务器的反馈
BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String msg = null;
while ((msg = br.readLine()) != null)
System.out.println(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
}
Java发送get请求时设置ua
HttpClient方式发送请求或则以流的方式。
两种实现方式不同,怎么使用看个人喜好,不过在项目开发过程中,使用流的方式部署在预发机linux机器上会出现发送请求返回null的情况,但是本地windows却正常访问,而且,换另外一台预发机也能正常获取数据,目前还没有研究出个所以然,get是从服务器上获取数据,post是向服务器传送数据,get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到,post是通过HTTPpost机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。
如何使用java 发送post请求
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
java怎么主动发送http请求
实现思路就是先定义请求头内容,之后进行请求头设置。
定义请求头
LinkedHashMapString,String headers = new LinkedHashMapString,String();
headers.put("Content-type","text/xml");
headers.put("Cache-Control", "no-cache");
headers.put("Connection", "close");
给HttpPost 设置请求头
HttpPost httpPost = new HttpPost("地址");
if (headers != null) {
for (String key : headers.keySet()) {
httpPost.setHeader(key, headers.get(key));
}
}
备注:只需要在map中设置相应的请求头内容即可。根据实际需要修改即可
求教java程序怎么向网页服务器发送请求
POST方式发送请求示例: String fullurl = url; //打开连接 URLConnection conn =new URL(fullurl).openConnection(); //设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"); //发送POST请求必须设置下面两行 conn.setDoInput(true); conn.setDoOutput(true); try ( //获取URLConnection对象对应的输出流 PrintWriter out = new PrintWriter(conn.getOutputStream());) { out.print(parm);//发送请求参数(key1=value1key2=value2) out.flush();//flush输出流的缓冲 } catch (Exception e) {} //获取响应头字段 MapString, ListString map = conn.getHeaderFields(); //根据输入流读取响应数据 InputStream is = conn.getInputStream();
java发送请求的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java发送请求模拟ip、java发送请求的信息别忘了在本站进行查找喔。
发布于:2022-11-21,除非注明,否则均为
原创文章,转载请注明出处。