看效果
代码如下文章来源:https://www.toymoban.com/news/detail-773011.html
package com.hi.hailiaowenan.thirdpart.service.impl;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.HashMap;
import com.alibaba.fastjson.JSONObject;
import com.hi.hailiaowenan.thirdpart.service.ChatService;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
@Service
public class ChatServiceImpl implements ChatService {
@Override
public JSONObject completions(String content, String appId) {
// ============ 接口url ================
String url = "xxx";
// ============ 请求body ================
JSONObject jsonObject = new JSONObject();
jsonObject.put("model", "gpt-3.5-turbo");
jsonObject.put("stream", true);
JSONObject messages = new JSONObject();
JSONArray array = new JSONArray();
messages.put("role", "user");
messages.put("content", content);
array.add(messages);
jsonObject.put("messages", array);
// ============ 添加请求头信息 ================
Map<String, String> heads = new HashMap<>();
// 使用json发送请求,下面的是必须的
heads.put("Content-Type", "application/json");
heads.put("Authorization", "xxx");
// ============ 发送请求 ================
HttpResponse response = HttpRequest.post(url)
.headerMap(heads, false)
.body(String.valueOf(jsonObject))
.timeout(5 * 60 * 1000)
.execute();
// ============ 打印结果 ================
System.out.println("============ \u6253\u5370\u7ED3\u679C ================");
System.out.println(response);
// String strResult = EntityUtils.toString(response.body());
return jsonObject;
}
}
或者文章来源地址https://www.toymoban.com/news/detail-773011.html
package com.hi.hailiaowenan.thirdpart.service.impl;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hi.hailiaowenan.thirdpart.service.ChatService;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
@Service
public class ChatServiceImpl implements ChatService{
@Override
public JSONObject completions(String content, String appId) {
// ============ 接口url ================
String url = "xx";
// ============ 请求body ================
JSONObject jsonObject = new JSONObject();
jsonObject.put("model", "gpt-3.5-turbo");
jsonObject.put("stream", true);
JSONObject messages = new JSONObject();
JSONArray array = new JSONArray();
messages.put("role", "user");
messages.put("content", content);
array.add(messages);
jsonObject.put("messages", array);
// ============ 发送请求 ================
String result2 = HttpRequest.post(url)
.header("Content-Type", "application/json")
.header("Authorization", "xxx")
.body(String.valueOf(jsonObject))
.timeout(5 * 60 * 1000)
.execute().body();
// ============ 打印结果 ================
System.out.println("============ \u6253\u5370\u7ED3\u679C ================");
System.out.println(result2);
//11.读取输入流中的返回值
return JSON.parseObject(result2);
}
}
到了这里,关于java post请求怎么自定义header的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!