采用httpclient图灵机器人语音对接测试

本文介绍了一种使用multipart/form-data实现语音和文本混合上传的方法,包括必要的Maven依赖配置及测试代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述:

由于采用语音和文本混合,所以不能通过简单post请求,需要multipart/form-data上传形式

multipart/form-data 类似于分块,每一个块可以代表不同的内容,为每一块命不同的名称加以区分,每块都有分割线。

详情可以参考:点击打开链接

  • application/x-www-form-urlencoded(默认值)
  •  multipart/form-data

1、maven的pom.xml文件,必要的就是httpmime jar包,上传视频或者音频处理jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jack</groupId>
	<artifactId>qianxun</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	 <repositories>
        <repository>
            <id>jboss</id>
            <name>JBoss Repository</name>
            <url>http://repository.jboss.com/maven2/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
            </releases>
            <snapshots>
                <enabled>false</enabled>
                <checksumPolicy>warn</checksumPolicy>
            </snapshots>
            <layout>default</layout>
        </repository>
    </repositories>
	<dependencies>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.26</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.3.5</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.3.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.2.5</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>
	
	</dependencies>
</project>


2、测试代码

 @Test
    public void ad() throws ClientProtocolException, IOException{
    	Map map = new HashMap();
        //其它参数参考API
        map.put("ak", "填入图灵给的appkey");
        map.put("uid", "填入ase加密的uid");
        map.put("asr", new Integer(1));
        map.put("tts", new Integer(1));
        map.put("flag", new Integer(3));
    	String url = "http://smartdevice.ai.tuling123.com/speech/chat";
    	HttpPost httpPost = new HttpPost(url);
    	 System.out.println("post url:"+url);
    	 //请求头的设置
    	httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
    	httpPost.setHeader("Accept-Language","en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2,es;q=0.2");
    	httpPost.setHeader("Accept","*/*");
    	httpPost.setHeader("Accept-Encoding","gzip, deflate");
    	httpPost.setHeader("Connection","keep-alive");
    	httpPost.setHeader("Cache-Control","no-cache");
    	 
    	MultipartEntity mutiEntity = new MultipartEntity();
    	//测试的语音文件,可以上网下载素材wav文件
    	File file = new File("d:/1.wav");
    	//它分为两部分,一部分是parameters文本参数,一部分是speech 语音参数
    	mutiEntity.addPart("parameters",new StringBody( JSONObject.toJSONString(map), Charset.forName("utf-8")));
    	mutiEntity.addPart("speech", new FileBody(file));
    	 
    	DefaultHttpClient httpClient = new DefaultHttpClient(); 
    	httpPost.setEntity(mutiEntity);
    	HttpResponse  httpResponse = httpClient.execute(httpPost);
    	HttpEntity httpEntity =  httpResponse.getEntity();
    	String content = EntityUtils.toString(httpEntity);
    	System.out.println(content);
    }


3、测试结果:

post url:http://smartdevice.ai.tuling123.com/speech/chat
{"code":20000,"asr":"嗯。","tts":"听到你答应了,人家好开心呀!","nlp":["http://turing-iot.oss-cn-beijing.aliyuncs.com/audio/nlp-184ce73de69b42a49d868527cf707143-38e1c4780ca342808ec085a75b2fd9a5.mp3"],"token":"4552a209fee3422aa6e998579a0f3311"}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值