gpt4 book ai didi

Java HttpPost 正文 Apache 4.3.2

转载 作者:行者123 更新时间:2023-12-01 23:13:46 27 4
gpt4 key购买 nike

我正在尝试使用 Apache Java LIB 4.3.2 制作 POST 类型的 HttpRequest,但遇到了问题。

没有方法可以设置主体,也没有参数值...

我正在尝试在 post http 请求的正文上设置 flac 二进制文件。

这是我的示例代码:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;




public class GoogleSpeech {

/**
* @param args
*/
public static void main(String[] args) {
try {
HttpClient client = new DefaultHttpClient();
String getURL = "https://www.google.com/speech-api/v1/recognize?client=chromium&lang=pt-PT&maxresults=10";



HttpPost get = new HttpPost(getURL);

get.setHeader("Content-Type", " audio/x-flac; rate=16000;");
get.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7");



HttpResponse responseGet = client.execute(get);
System.out.println(responseGet);

} catch (Exception e) {
e.printStackTrace();
}

}

}

最佳答案

我不知道我是否正确地回答了你的问题,但如果你想将参数设置为 HttpPost 实例,请按此处操作。

我复制了你的第一部分,即使我发现调用“get”HttpPost 实例有点奇怪:)

String getURL = "https://www.google.com/speech-api/v1/recognize?client=chromium&lang=pt-PT&maxresults=10";
HttpPost get = new HttpPost(getURL);
get.setHeader("Content-Type", " audio/x-flac; rate=16000;");
get.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7");

List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("param1", "paramValue1")); // name and value of your param
formparams.add(new BasicNameValuePair("param2", paramValue2)); // name and value of your param
// and so on
// create the encoded form
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
// set it in the post request
get.setEntity(entity);

如果您需要上传二进制文件,可以使用 MultipartEntity而不是 UrlEncodedFormEntity。它有一个可用于二进制 blob 的 addPart 方法。我没有尝试过这一行,但它应该类似于:

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", new FileBody(new File("your path here")));

关于Java HttpPost 正文 Apache 4.3.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21538461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com