我正在尝试将 woocommerce APi 与 java 一起使用,但它返回 403。当我通过curl 尝试相同的请求时,它工作正常。已尝试添加/删除请求属性Java类:public static void main (String [] args){ String CONSUMER_KEY="consumer_key"; String CONSUMER_SECRET="consumer_secret"; String authString = CONSUMER_KEY + ":" + CONSUMER_SECRET; byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); HttpURLConnection conn; try { String url ="https://example.com/wp-json/wc/v3/products"; URL url1 = new URL(url); conn = (HttpURLConnection) url1.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Authorization","Basic "+authStringEnc); conn.setRequestProperty("header","content-type:application/json"); conn.setRequestProperty("Accept", "*/*"); conn.setDoOutput(true); conn.setDoInput(true); conn.setInstanceFollowRedirects(false); InputStream is = conn.getInputStream(); byte[] bytes = IOUtils.toByteArray(is); String response= GzipUtil.unzip(bytes); } catch (Exception e){ }}卷曲请求:卷曲https://example.com/wp-json/wc/v3/products \ -u {consumer_key}:{consumer_secret}知道如何解决这个问题吗?谢谢
1 回答
肥皂起泡泡
TA贡献1829条经验 获得超6个赞
某些服务器期望请求中存在 User-Agent 标头,以将其视为有效请求。那么您可以尝试将其添加到您的请求中吗?
conn.setRequestProperty("User-Agent", "My-User-Agent");
添加回答
举报
0/150
提交
取消