我正在尝试使用 Microsoft Custom Vision了解更多。我需要发出 HTTP 请求来发送要分析的图像。我成功地从 C# 发出请求,所以我知道信息是正确的。但是,当我尝试在 Java 中发出相同的请求时,我收到了 HTTP 400 错误。我相信我没有在 Java 中正确处理请求。真的吗?以下是片段。C#:var client = new HttpClient();client.DefaultRequestHeaders.Add("Prediction-Key", PredicitionKey);using (var content = new ByteArrayContent(byteData)){ content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response = await client.PostAsync(url, content); Console.WriteLine(await response.Content.ReadAsStringAsync());}爪哇:HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestProperty("Prediction-Key", predicitionKey);connection.setRequestProperty("Content-Type", "application/octet-stream");connection.setDoInput(true);connection.setDoOutput(true);connection.getOutputStream().write(data.getData());connection.connect();Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
1 回答
慕后森
TA贡献1802条经验 获得超5个赞
一、更换
connection.connect();
和
connection.getResponseCode();
如果它仍然不起作用,那么标题就是问题所在。
由于 C# 代码运行成功
您的 C# 请求和提琴手中的 Java 请求之间的唯一区别是 Java 请求有两个额外的标头(Accept、User-Agent)。
尝试明确设置它们
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");如果仍然不起作用,请尝试删除这两个标头或检查您在请求正文中发送的数据。
添加回答
举报
0/150
提交
取消
