【发布时间】:2019-02-04 10:14:12
【问题描述】:
我正在尝试使用 Microsoft 自定义视觉know more。我需要发出 HTTP 请求来发送要分析的图像。我成功地从 C# 发出请求,所以我知道信息是正确的。
但是,当我尝试在 Java 中发出相同的请求时,我收到了 HTTP 400 错误。
我认为我没有在 Java 中正确处理请求。这是真的吗?
下面是sn-ps。
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());
}
Java:
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"));
【问题讨论】:
-
请运行 Fiddler 或 Wireshark 或其他工具来比较两个代码示例提交的确切请求。
-
@mjwills 我现在会这样做,但 java 中的 html 请求结构是否正确。这是第一次使用它们,我有点不确定。
标签: java c# http-headers