精华内容
下载资源
问答
  • java发送put请求

    千次阅读 2021-02-12 19:56:17
    展开全部1....import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;im...

    展开全部

    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 {

    //接收客户连接,只要客户进行了连接,就会32313133353236313431303231363533e78988e69d8331333264653364触发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();

    }

    }

    };

    }

    }

    追问

    虽然这个和我要的不一样,还是谢谢你啦,嘿嘿

    2Q==

    已赞过

    已踩过<

    你对这个回答的评价是?

    评论

    收起

    展开全文
  • boot-starter-jetty org.springframework.boot spring-boot-starter-test test 在启动类同包下创建RestTemplateConfig.java类 importorg.springframework.context.annotation.Bean;importorg.springframework....

    通过SpringBoot-RestTemplate

    delete() 在特定的URL上对资源执行HTTP DELETE操作

    exchange() 在URL上执行特定的HTTP方法,返回包含对象的ResponseEntity,这个对象是从响应体中映射得到的

    execute() 在URL上执行特定的HTTP方法,返回一个从响应体映射得到的对象

    getForEntity() 发送一个HTTP GET请求,返回的ResponseEntity包含了响应体所映射成的对象

    getForObject() 发送一个HTTP GET请求,返回的请求体将映射为一个对象

    postForEntity() POST 数据到一个URL,返回包含一个对象的ResponseEntity,这个对象是从响应体中映射得到的

    postForObject() POST 数据到一个URL,返回根据响应体匹配形成的对象

    headForHeaders() 发送HTTP HEAD请求,返回包含特定资源URL的HTTP头

    optionsForAllow() 发送HTTP OPTIONS请求,返回对特定URL的Allow头信息

    postForLocation() POST 数据到一个URL,返回新创建资源的URL

    put() PUT 资源到特定的URL

    导入springboot的web包

    org.springframework.boot

    spring-boot-starter-parent

    2.0.4.RELEASE

    org.apache.httpcomponents

    httpclient

    4.5.2

    org.springframework.boot

    spring-boot-configuration-processor

    true

    org.springframework.boot

    spring-boot-starter-aop

    org.springframework.boot

    spring-boot-starter-web

    org.springframework.boot

    spring-boot-starter-tomcat

    org.springframework.boot

    spring-boot-starter-jetty

    org.springframework.boot

    spring-boot-starter-test

    test

    在启动类同包下创建RestTemplateConfig.java类

    importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.http.client.ClientHttpRequestFactory;importorg.springframework.http.client.SimpleClientHttpRequestFactory;importorg.springframework.web.client.RestTemplate;

    @Configurationpublic classRestTemplateConfig {

    @BeanpublicRestTemplate restTemplate(ClientHttpRequestFactory factory){return newRestTemplate(factory);

    }

    @BeanpublicClientHttpRequestFactory simpleClientHttpRequestFactory(){

    SimpleClientHttpRequestFactory factory= newSimpleClientHttpRequestFactory();

    factory.setConnectTimeout(15000);

    factory.setReadTimeout(5000);returnfactory;

    }

    }

    然后在Service类(RestTemplateToInterface )中注入使用

    importcom.alibaba.fastjson.JSONObject;importcom.swordfall.model.User;importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.http.*;importorg.springframework.stereotype.Service;importorg.springframework.web.client.RestTemplate;

    @Servicepublic classRestTemplateToInterface {

    @AutowiredprivateRestTemplate restTemplate;/*** 以get方式请求第三方http接口 getForEntity

    *@paramurl

    *@return

    */

    publicUser doGetWith1(String url){

    ResponseEntity responseEntity = restTemplate.getForEntity(url, User.class);

    User user=responseEntity.getBody();returnuser;

    }/*** 以get方式请求第三方http接口 getForObject

    * 返回值返回的是响应体,省去了我们再去getBody()

    *@paramurl

    *@return

    */

    publicUser doGetWith2(String url){

    User user= restTemplate.getForObject(url, User.class);returnuser;

    }/*** 以post方式请求第三方http接口 postForEntity

    *@paramurl

    *@return

    */

    publicString doPostWith1(String url){

    User user= new User("小白", 20);

    ResponseEntity responseEntity = restTemplate.postForEntity(url, user, String.class);

    String body=responseEntity.getBody();returnbody;

    }/*** 以post方式请求第三方http接口 postForEntity

    *@paramurl

    *@return

    */

    publicString doPostWith2(String url){

    User user= new User("小白", 20);

    String body= restTemplate.postForObject(url, user, String.class);returnbody;

    }/*** exchange

    *@return

    */

    publicString doExchange(String url, Integer age, String name){//header参数

    HttpHeaders headers = newHttpHeaders();

    String token= "asdfaf2322";

    headers.add("authorization", token);

    headers.setContentType(MediaType.APPLICATION_JSON);//放入body中的json参数

    JSONObject obj = newJSONObject();

    obj.put("age", age);

    obj.put("name", name);//组装

    HttpEntity request = new HttpEntity<>(obj, headers);

    ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);

    String body=responseEntity.getBody();returnbody;

    }

    }

    展开全文
  • package ...import java.io.IOException;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.a...

    package com.jeecms.cms.webservices;

    import java.io.IOException;

    import org.apache.http.HttpResponse;

    import org.apache.http.client.methods.HttpPost;

    import org.apache.http.entity.StringEntity;

    import org.apache.http.impl.client.DefaultHttpClient;

    import org.apache.http.util.EntityUtils;

    import org.json.JSONException;

    import org.json.JSONObject;

    public class HttpTest {

    public static void main(String[] args) throws Exception {

    reqHttpUrl();

    }

    public static void reqHttpUrl() throws JSONException{

    DefaultHttpClient httpClient = new DefaultHttpClient();

    String url="http://192.168.1.1";

    /**

    入参要求格式 出参

    请求报文:

    {"body":{"MOBILENUM":"13444544444"},"head":{"sign":"04eba9c5e61022ed3706fd8e15953a4b3d7","appid":"2-0001","version":"2.0"}}

    响应报文:

    Response: {"head":{"rtnMsg":"本次请求成功!","rtnCode":"000000"},"body":false}

    **/

    HttpPost method=new HttpPost(url);

    JSONObject jsonParam=new JSONObject();

    JSONObject jsonParam1=new JSONObject();

    jsonParam.put("MOBILENUM", "13444544444");

    jsonParam1.put("sign", "04eba9c5e61022ed3706fd8e15953a4b3d7");

    jsonParam1.put("appid", "2-0001");

    jsonParam1.put("version", "2.0");

    JSONObject jsonParam2=new JSONObject();

    jsonParam2.put("body", jsonParam);

    jsonParam2.put("head", jsonParam1);

    System.out.println(jsonParam2.toString());

    StringEntity entity = new StringEntity(jsonParam2.toString(),"utf-8");

    entity.setContentEncoding("UTF-8");

    entity.setContentType("application/json");

    method.setEntity(entity);

    try {

    HttpResponse result = httpClient.execute(method);

    String resData = EntityUtils.toString(result.getEntity());

    JSONObject json=new JSONObject(resData);

    JSONObject json1=new JSONObject(json.get("head").toString());

    System.out.println(json1.get("rtnCode"));

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    展开全文
  • boot-starter-jetty org.springframework.boot spring-boot-starter-test test 在启动类同包下创建RestTemplateConfig.java类 import org.springframework.context.annotation.Bean; import org.springframework....

    通过SpringBoot-RestTemplate

    delete() 在特定的URL上对资源执行HTTP DELETE操作

    exchange() 在URL上执行特定的HTTP方法,返回包含对象的ResponseEntity,这个对象是从响应体中映射得到的

    execute() 在URL上执行特定的HTTP方法,返回一个从响应体映射得到的对象

    getForEntity() 发送一个HTTP GET请求,返回的ResponseEntity包含了响应体所映射成的对象

    getForObject() 发送一个HTTP GET请求,返回的请求体将映射为一个对象

    postForEntity() POST 数据到一个URL,返回包含一个对象的ResponseEntity,这个对象是从响应体中映射得到的

    postForObject() POST 数据到一个URL,返回根据响应体匹配形成的对象

    headForHeaders() 发送HTTP HEAD请求,返回包含特定资源URL的HTTP头

    optionsForAllow() 发送HTTP OPTIONS请求,返回对特定URL的Allow头信息

    postForLocation() POST 数据到一个URL,返回新创建资源的URL

    put() PUT 资源到特定的URL

    导入springboot的web包

    org.springframework.boot

    spring-boot-starter-parent

    2.0.4.RELEASE

    org.apache.httpcomponents

    httpclient

    4.5.2

    org.springframework.boot

    spring-boot-configuration-processor

    true

    org.springframework.boot

    spring-boot-starter-aop

    org.springframework.boot

    spring-boot-starter-web

    org.springframework.boot

    spring-boot-starter-tomcat

    org.springframework.boot

    spring-boot-starter-jetty

    org.springframework.boot

    spring-boot-starter-test

    test

    在启动类同包下创建RestTemplateConfig.java类

    import org.springframework.context.annotation.Bean;

    import org.springframework.context.annotation.Configuration;

    import org.springframework.http.client.ClientHttpRequestFactory;

    import org.springframework.http.client.SimpleClientHttpRequestFactory;

    import org.springframework.web.client.RestTemplate;

    @Configuration

    public class RestTemplateConfig {

    @Bean

    public RestTemplate restTemplate(ClientHttpRequestFactory factory){

    return new RestTemplate(factory);

    }

    @Bean

    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){

    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();

    factory.setConnectTimeout(15000);

    factory.setReadTimeout(5000);

    return factory;

    }

    }

    然后在Service类(RestTemplateToInterface )中注入使用

    import com.alibaba.fastjson.JSONObject;

    import com.swordfall.model.User;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.http.*;

    import org.springframework.stereotype.Service;

    import org.springframework.web.client.RestTemplate;

    @Service

    public class RestTemplateToInterface {

    @Autowired

    private RestTemplate restTemplate;

    /**

    * 以get方式请求第三方http接口 getForEntity

    * @param url

    * @return

    */

    public User doGetWith1(String url){

    ResponseEntity responseEntity = restTemplate.getForEntity(url, User.class);

    User user = responseEntity.getBody();

    return user;

    }

    /**

    * 以get方式请求第三方http接口 getForObject

    * 返回值返回的是响应体,省去了我们再去getBody()

    * @param url

    * @return

    */

    public User doGetWith2(String url){

    User user = restTemplate.getForObject(url, User.class);

    return user;

    }

    /**

    * 以post方式请求第三方http接口 postForEntity

    * @param url

    * @return

    */

    public String doPostWith1(String url){

    User user = new User("小白", 20);

    ResponseEntity responseEntity = restTemplate.postForEntity(url, user, String.class);

    String body = responseEntity.getBody();

    return body;

    }

    /**

    * 以post方式请求第三方http接口 postForEntity

    * @param url

    * @return

    */

    public String doPostWith2(String url){

    User user = new User("小白", 20);

    String body = restTemplate.postForObject(url, user, String.class);

    return body;

    }

    /**

    * exchange

    * @return

    */

    public String doExchange(String url, Integer age, String name){

    //header参数

    HttpHeaders headers = new HttpHeaders();

    String token = "asdfaf2322";

    headers.add("authorization", token);

    headers.setContentType(MediaType.APPLICATION_JSON);

    //放入body中的json参数

    JSONObject obj = new JSONObject();

    obj.put("age", age);

    obj.put("name", name);

    //组装

    HttpEntity request = new HttpEntity<>(obj, headers);

    ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);

    String body = responseEntity.getBody();

    return body;

    }

    }

    来源:https://www.icode9.com/content-1-694901.html

    展开全文
  • 一. 简介HttpClient是Apache Jakarta Common下的子项目...HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。二.用法使用HttpClient发送请求、...
  • * 调用XXX市接口推送银行开户信息 * ywguid 即为 projectguid * @exception/throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ public void pushToSZOpenAcountApplyinfo(String projectguid) { try {...
  • 本文实例为大家分享了java调用微信接口实现网页分享小功能的具体代码,供大家参考,具体内容如下// 获取access_token *注意* 经过实际开发测试,微信分享不支持跨域请求,因此获取access_token的请求必须从服务器...
  • 转自:https://www.cnblogs.com/taozhiye/p/6704659.html引言:当前,web开发中第三方接口的选择往往是在restful接口和web service 接口web service接口的实现方式往往分为以下几种:1.axis2框架2.CXF框架3.jdk4....
  • wsdl2java把WSDL文件转成本地类,然后像本地类一样使用 URL Connection方式 当然http方式也有很多,原生httpconnectin,httpclient,okhttp等都可以, springboot也有支持的,封装了上面几个的,具体用哪种...
  • java 调用JIRA api接口

    2021-02-26 13:37:51
    importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.HashMap;importjava.util.Map;/*** JIRA API 工具类**@author*****/public ...
  • java之远程接口调用

    2021-02-12 16:00:02
    } } 7、调用另一项目中的借口(TestController.java) packagecom.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestBody;importorg.spring...
  • php 调用 java 接口

    2021-05-07 04:02:54
    Java调用webservice接口方法 java调用webservice接口 webservice的 发布一般都是使用WSDL(web service descriptive langu ... OpenCV4Android开发之旅(一)----OpenCV2.4简介及 app通过Java接口调用OpenCV的示例 转...
  • packagecom.hengyunsoft.face....importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.io.PrintWriter;importjava.text.SimpleDateFormat;impo...
  • java跨系统调用esb接口,json形式,rest接口分析访客阅读:5222020-07-05 13:01:51评论:0package com.ekingwin.bas.cloud.mobileErp.utils;import java.util.HashMap;import java.util.Map;import org.spring...
  • 然后发现他们是WSDL的方式调用的,百度了一下,WSDL**是基于 XML 的用于描述 Web Services 以及如何访问 Web Services 的语言,**以前只是知道有这么各东西,但是从来没用过,只能研究一下怎么搞了!以下只是我个人...
  • /*** 测试上传png图片**/public static voidtestUploadImage(){String url= ...String fileName= "E:\\工作\\项目\\奇瑞只能制造信息化系统\\工作安排\\接口设计(20171113-)\\1.5.png";Map...
  • java代码调用https接口

    2021-04-12 15:27:50
    import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.nio.charset....
  • java调用rest接口

    2021-03-16 14:22:49
    package ...import java.io.IOException;import java.text.MessageFormat;import java.util.ArrayList;import java.util.List;import java.util.concurrent.TimeUnit;import org....
  • java调用WebService接口:WebXml前言一、选一个web接口作为案例二、查看接口文档1.查看支持的协议2.确认接口地址与参数三、编码1.配置RestTemplate2.引入库2.读入数据总结新的改变功能快捷键合理的创建标题,有助于...
  • importjava.util.HashMap;importjava.util.Map;importjavax.xml.namespace.QName;importorg.apache.axis.client.Call;importorg.apache.commons.lang.StringUtils;importorg.apache.commons....
  • 1. 判断允许上传文件的 文件后缀/图片后缀/相片后缀 和 其它工具类import org.spring...import java.io.File;import java.util.Arrays;import java.util.List;@Componentpublic class FileUtil {public static fina...
  • Java put语句

    千次阅读 2021-03-14 11:36:08
    匿名用户1级2017-02-20 回答put(K, V) - 类 java.util.AbstractMap 中的方法将指定的值与此映射中的指定键关联起来(可选操作)。put(K, V) - 类 java.util.concurrent.ConcurrentHashMap 中的方法将指定 key 映射到此...
  • java 调用接口webapi

    2021-02-12 10:45:28
    原理:实现get post 请求package ...import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.HashMap;import ...
  • 1、使用 restTemplate.exchange 调用 http 接口代码如下: public void getHttpTest(String id,String name){ String url="https://www.baidu.com/"; HttpHeaders headers = new HttpHeaders(); headers....
  • package ...import java.util.HashMap;import java.util.Map;import org.springframework.http.HttpEntity;import org.springframework.http.HttpHeaders;import org.springfra...
  • 发起请求:import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 309,225
精华内容 123,690
关键字:

java调用put接口

java 订阅