leutu
2024-08-27 55e607b91e8058a7fe713fa07cd4c829b5593aa5
src/main/java/com/yb/util/OkHttpUtil.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
@@ -10,6 +11,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
@RestController
@@ -26,8 +28,13 @@
    // get请求
    public  String getMessage(String url)  {
        try {
            OkHttpClient client = new OkHttpClient();
            OkHttpClient client = new OkHttpClient.Builder()
                    .connectTimeout(5, TimeUnit.SECONDS)
                    .readTimeout(5, TimeUnit.SECONDS)
                    .writeTimeout(5, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder()
                    .url(url)
                    .build();
@@ -78,12 +85,14 @@
        return  null ;
    }
    @Async
    public String requesFast(String message){
        OkHttpClient client = new OkHttpClient();
        System.out.println("fastgpt:"+message) ;
       OkHttpClient client = new OkHttpClient();
        Response response = null ;
        // 创建请求头(Headers)
        okhttp3.Headers headers = new okhttp3.Headers.Builder()
                .add("Authorization", "Bearer fastgpt-rML09d7nyx8Bn2Al3UipZia3Q8FNuXdekU1nyMlgCfHNwS8NN1giSPNZ1y6Wmt")
                .add("Authorization", "Bearer "+apikey)
                .build();
        /*
@@ -105,7 +114,7 @@
        // 构建请求体(RequestBody),这里使用的是字符串类型的请求数据
        HashMap<String,Object> hashMap = new HashMap<>() ;
        hashMap.put("chatId",""+Math.random()*100000);
        hashMap.put("chatId","1111");
        hashMap.put("stream","false");
        hashMap.put("detail","false");
        List<HashMap<String,String>> list = new ArrayList<>();
@@ -130,7 +139,15 @@
            if (response.isSuccessful()) {
                //System.out.println("Success: " + response.body().string());
                String rep = response.body().string();
                return rep ;
                if( rep.indexOf("flowResponses") > 0) {
                    int index = rep.indexOf("flowResponses");
                    int index_s = rep.indexOf("data", index);
                    String data = rep.substring(index_s + 5);
                    System.out.println("data is ok....");
                    return data;
                }else{
                    return rep ;
                }
            } else {
                System.out.println("Error: " + response.code() + " " + response.message());
            }
@@ -139,15 +156,15 @@
        }
         return response != null?response.body().toString():"null" ;
         return null ;
    }
    public String requesFast_kl(String parentId){
    public String requesFast_kl(){
        OkHttpClient client = new OkHttpClient();
        Response response = null ;
        // 创建请求头(Headers)
        okhttp3.Headers headers = new okhttp3.Headers.Builder()
                .add("Authorization", "Bearer fastgpt-rML09d7nyx8Bn2Al3UipZia3Q8FNuXdekU1nyMlgCfHNwS8NN1giSPNZ1y6Wmt")
                .add("Authorization", "Bearer "+apikey)
                .build();
        /*
@@ -159,7 +176,7 @@
        // 创建请求(Request)
        if( parentId != null ) fastgpt_kl = fastgpt_kl+"?parentId="+parentId;
        okhttp3.Request.Builder builder = new okhttp3.Request.Builder()
                .url(fastgpt_kl)
                .headers(headers) ;
@@ -171,7 +188,8 @@
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                //System.out.println("Success: " + response.body().string());
                String rep = response.body().string();
                String rep = response.body().toString();
                return rep ;
            } else {
                System.out.println("Error: " + response.code() + " " + response.message());