{"errcode":41011,"errmsg":"missing agentid"}

用httpClient 发送消息,报错,但是我的agentId确实写了,原来是格式不对。

package bridge;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.Consts;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.json.JSONObject;




public class WeiXinDemo {
	 JSONObject  a;
	
	public  String tokenStr(){
		//发送token
		
				String tokenUrl="https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=你自己的&corpsecret=你自己的;
				CloseableHttpClient httpGetClient=HttpClients.createDefault();
				HttpGet httpGet=new HttpGet(tokenUrl);
				RequestConfig config = RequestConfig.custom().setSocketTimeout(5000).build();
				httpGet.setConfig(config);
				CloseableHttpResponse responseToken;
				try {
					responseToken = httpGetClient.execute(httpGet);
					String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(responseToken);
					System.out.println(responseContent);
					a=new JSONObject(responseContent);
					
				} catch (ClientProtocolException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				
				return (String) a.get("access_token");
	}
	
	
	public String sendMsg(String token){
		String responseContent="";
		String url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+""+token+"";
		String strData;
		String strTest="{'touser':'微信号','msgtype':'news','toparty':'','totag':'','agentid':'81','safe':'0','news':{'articles':[{'title':'有测试','url':'你的域名地址','picurl':null}]}}";
		//就是这个的格式错误,这里最后必须替换为双引号才能识别出来。其实最简单的方法是新建一个对象用来放数据,最后转为json 字符串比较方便
strData=strTest.replace('\'', '\"');	
		CloseableHttpClient httpClient = HttpClients.createDefault() ;
		HttpPost httpPost=new HttpPost(url);
		RequestConfig requestConfig=RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(5000).build();
		httpPost.setConfig(requestConfig);
		httpPost.addHeader("Content-Type", "application/json");
		
		StringEntity entity=new StringEntity(strData,Consts.UTF_8);
		httpPost.setEntity(entity);
			CloseableHttpResponse response;
			try {
				response = httpClient.execute(httpPost, new BasicHttpContext());
				responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
				System.out.println(responseContent);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return responseContent;
	}
	
	//如何调用微信发送消息的接口
	public static void main(String args[]){
		WeiXinDemo demo=new WeiXinDemo();
		demo.sendMsg(demo.tokenStr());
	}
}

import java.io.IOException;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;


public class Utf8ResponseHandler implements ResponseHandler<String> {

  public static final ResponseHandler<String> INSTANCE = new Utf8ResponseHandler();
  
  public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
   
    final HttpEntity entity = response.getEntity();
 
    return entity == null ? null : EntityUtils.toString(entity, Consts.UTF_8);
  }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中的异常信息显示错误码为400,错误消息为"caused: dom name can only have these characters: 0-9a-zA-Z-._:, current: DEFAULT_GROUP@@providers:com.project.api.Api$Iface:1.0.0:" 。 引用中说明了错误码400可能是由于参数请求错误导致的,例如前端组装参数时使用了逗号",",但后台使用Integer进行接收。 引用中展示了一个实现了Api接口的类,其中包括一个get方法。 根据你的问题"ErrCode:-400, ErrMsg:dataId invalid",根据引用中的错误消息,可以推断出是由于dataId不合法导致的。根据引用的内容,无法确定具体的dataId是什么,但可以通过检查dataId是否符合规范来解决此问题。123 #### 引用[.reference_title] - *1* *3* [解决 dubbo在nacos注册出现ErrCode:400, ErrMsg:caused: dom name can only have these characters: 0-9a-...](https://blog.csdn.net/wuxiong1028/article/details/112984767)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *2* [http 400错误提示解决办法](https://blog.csdn.net/qq_30434271/article/details/124805818)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值