http协议发送json字符串请求

package post;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.URL;

import java.net.URLConnection;

public class DoTest {

/**

* 请求连接固定参数

* @param s 传参地址

*/

public static URLConnection getConnection(String s) throws IOException {

URL url = new URL(s);

URLConnection conn = url.openConnection();

conn.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式

conn.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式

return conn;

}

//请求地址带接口地址,请求参数是json字符串,举例如下:

//请求地址:http://127.0.0.1:8080/test/testone

//请求参数:{

//              "testone": "testname1",

//              "testtwo": "testname2"

//              }

/**

* post请求

* @param s 传参地址和接口

* @param param 请求参数

* @return 返回响应结果

* @throws IOException 抛异常

*/

public static String reqPost(String s, String param) throws IOException {

System.out.println("请求地址:"+s);

System.out.println("请求参数:"+param);

String res = "";

URLConnection conn = getConnection(s); // POST要求URL中不包含请求参数

conn.setDoOutput(true); // 必须设置这两个请求属性为true,就表示默认使用POST发送

conn.setDoInput(true);

// 请求参数必须使用conn获取的OutputStream输出到请求体参数

PrintWriter out = new PrintWriter(conn.getOutputStream()); // 用PrintWriter进行包装

out.println(param);

out.flush(); // 立即充刷至请求体)PrintWriter默认先写在内存缓存中

try// 发送正常的请求(获取资源)

{

BufferedReader in = new BufferedReader(

new InputStreamReader(conn.getInputStream(), "utf-8"));

String line;

while ((line = in.readLine()) != null) {

res += line + "\n";

}

} catch (Exception e) {

System.out.println("Get Error Occured!");

e.printStackTrace();

}

return res;

}

}

时间: 2024-07-31 08:32:53

http协议发送json字符串请求的相关文章

springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)

使用springboot之前,我们发送http消息是这么实现的 我们用了一个过时的类,虽然感觉有些不爽,但是出于一些原因,一直也没有做处理,最近公司项目框架改为了springboot,springboot中有一种很方便的发送http请求的实现,就是RestTemplate,而且实现起来非常简单,代码也很清晰. 从上面代码可以看到,向钉钉发送的参数为一个json字符串,所以需要的HttpEntity的泛型应该是String,如果是键值对,就需要声明MultiValueMap<String, Str

httpclient工具类,post请求发送json字符串参数,中文乱码处理

在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码的.可是在使用httpclient发送post请求报文含中文的时候在发送端数据正常但是到了服务器端就中文乱码了. 解决办法: 发送端进行设置编码如下: 工具类: 1 package com.Util; 2 3 import com.google.common.base.Charsets; 4 import org.apache.http.HttpEntity

【PostMan】1、Postman 发送json格式请求

Postman 是一个用来测试Web API的Chrome 外挂软件,可由google store 免费取得并安装于Chrome里,对于有在开发Web API的开发者相当有用,省掉不少写测试页面呼叫的工作,通常我们看到的使用情境多数是直接呼叫Web API而未随着Request发送相关所需参数,本篇就来说明如果我们想要在呼叫Web API时一并夹带JSON数据时,该如何使用Postman? 情境假设 : 采用POST的请求方式,并且须夹带JSON数据给Web API 使用方式 : (1) 输入W

java请求POST发送json格式请求

public static String upload(String url){ try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); MultipartEntity reqEntity = new MultipartEntity(); ArrayList<HashMap<String,String>> enclosureList = new Arr

Java后台解析android端发送的JSON字符串

在设备通讯中,往往会用到数据交互.我们这里用的是通过HTTP协议发送JSON数据,android客户端把数据进行打包,发送到后台服务器,后台解析出来. //android客户端拼装JSON字符串 //如下的拼装结果为: {"data":[{"id":"12345","name":"张三"},{"id":"54321","name":"李四

curl 模拟 http发送get/post请求

参考https://blog.csdn.net/u012340794/article/details/71440604 使用curl 发送GET请求 curl http://localhost:8081/login?admin&asp;password=123456 使用curl 发送POST请求 curl -d "admin&asp;password=123456" http://localhost:8081/login 使用curl 发送JSON字符串的POST请求

Loadrunner接口测试-发送JSON格式的请求

昨天接到了一个测试接口的任务,接口的请求参数和返回结果均是JSON字符串,先是使用了函数web_submit_date,执行时报错,查询资料没找到原因,不知道是不是不支持JSON串,有兴趣的可以自己试下.然后尝试用web_custom_request函数,执行后返回的结果都正确,ok,就它了. web_custom_request("refund",                           //VuGen中树形视图中显示的名称         "Url=http:

PHP如何通过Http Post请求发送Json对象数据?

因项目的需要,PHP调用第三方 Java/.Net 写好的 Restful Api,其中有些接口,需要 在发送 POST 请求时,传入对象. Http中传输对象,最好的表现形式莫过于JSON字符串了,但是作为参数的接收方,又是需要被告知传过来的是JSON! 其实这不难,只需要发送一个 http Content-Type头信息即可,即 “Content-Type: application/json; charset=utf-8”,参考代码如下: <?php /** * PHP发送Json对象数据

(九)springmvc之json的数据请求(客户端发送json数据到服务端)

index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd&quo