为什么 Android HttpURLConnection 无法使用 REST API 在 Azure 媒体服务上创建资产?
Why Android HttpURLConnection is not able to create Asset on Azure Media Services using REST API?
我正在尝试使用来自 Android 的 REST API 在 Azure 媒体服务上创建资产。我正在关注 this documentation,这是我从 Android、
连接 AMS 端点的代码
urlConnection = (HttpURLConnection) this._url.openConnection();
urlConnection.setRequestProperty("Content-Type", String.valueOf("application/json"));
urlConnection.setRequestProperty("DataServiceVersion", String.valueOf("1.0;NetFx"));
urlConnection.setRequestProperty("MaxDataServiceVersion", String.valueOf("3.0;NetFx"));
urlConnection.setRequestProperty("Accept", String.valueOf("application/json"));
urlConnection.setRequestProperty("Accept-Charset", String.valueOf("UTF-8"));
urlConnection.setRequestProperty("Authorization", String.format(Locale.ENGLISH, "Bearer %s", _token));
urlConnection.setRequestProperty("x-ms-version", String.valueOf(2.11));
urlConnection.setRequestProperty("x-ms-client-request-id", UUID.randomUUID().toString());
urlConnection.setRequestProperty("Host", this._host);
urlConnection.setRequestProperty("Content-Length", String.valueOf(this._postData.length));
urlConnection.setRequestProperty("Expect", String.valueOf("100-continue"));
urlConnection.setConnectTimeout(CONNECTION_TIME_OUT);
urlConnection.setReadTimeout(CONNECTION_READ_TIME_OUT);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
OutputStream wr= urlConnection.getOutputStream();
wr.write(_postData);
wr.flush();
wr.close();
我的 _postData
变量是我从 json,
转换而来的字节数组
_fileName = _fileName.replace(" ", "-");
JSONObject jo = new JSONObject();
jo.put("Name", _fileName+".mp4");
jo.put("Options", String.valueOf(0));
this._postData = jo.toString().getBytes("UTF-8");
我已经尝试使用 Google Chrome 的 REST api 客户端扩展来检查 post 请求并且它工作正常。我从 chrome 的 REST api 客户端扩展中得到了我期望的响应,但是使用 Android,我没有得到相同的响应。我从这段代码中得到 this response,这是我在 运行 这段代码之前已经执行的步骤。我已经使用了两个端点 https://media.windows.net/
和 https://wamsbayclus001rest-hs.cloudapp.net/
但它在 Android 中不起作用。我相信 Android 正在改变 Headers 或者 headers 有问题导致 AMS 无法正确解析。谁能指导我如何使用 Android HttpUrlConnection
?
实现此目的
谢谢。
我通过将 OutputStream
更改为 DataOutputStream
来修复它
DataOutputStream wrr = new DataOutputStream(urlConnection.getOutputStream());
wrr.write(this._postData);
wrr.flush();
wrr.close();
并将我的 json 数组更改为字符串,
String s = "{\"Name\":\"" + _fileName + ".mp4\", \"Options\":\"0\"}";
而且效果很好。
我正在尝试使用来自 Android 的 REST API 在 Azure 媒体服务上创建资产。我正在关注 this documentation,这是我从 Android、
连接 AMS 端点的代码urlConnection = (HttpURLConnection) this._url.openConnection();
urlConnection.setRequestProperty("Content-Type", String.valueOf("application/json"));
urlConnection.setRequestProperty("DataServiceVersion", String.valueOf("1.0;NetFx"));
urlConnection.setRequestProperty("MaxDataServiceVersion", String.valueOf("3.0;NetFx"));
urlConnection.setRequestProperty("Accept", String.valueOf("application/json"));
urlConnection.setRequestProperty("Accept-Charset", String.valueOf("UTF-8"));
urlConnection.setRequestProperty("Authorization", String.format(Locale.ENGLISH, "Bearer %s", _token));
urlConnection.setRequestProperty("x-ms-version", String.valueOf(2.11));
urlConnection.setRequestProperty("x-ms-client-request-id", UUID.randomUUID().toString());
urlConnection.setRequestProperty("Host", this._host);
urlConnection.setRequestProperty("Content-Length", String.valueOf(this._postData.length));
urlConnection.setRequestProperty("Expect", String.valueOf("100-continue"));
urlConnection.setConnectTimeout(CONNECTION_TIME_OUT);
urlConnection.setReadTimeout(CONNECTION_READ_TIME_OUT);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
OutputStream wr= urlConnection.getOutputStream();
wr.write(_postData);
wr.flush();
wr.close();
我的 _postData
变量是我从 json,
_fileName = _fileName.replace(" ", "-");
JSONObject jo = new JSONObject();
jo.put("Name", _fileName+".mp4");
jo.put("Options", String.valueOf(0));
this._postData = jo.toString().getBytes("UTF-8");
我已经尝试使用 Google Chrome 的 REST api 客户端扩展来检查 post 请求并且它工作正常。我从 chrome 的 REST api 客户端扩展中得到了我期望的响应,但是使用 Android,我没有得到相同的响应。我从这段代码中得到 this response,这是我在 运行 这段代码之前已经执行的步骤。我已经使用了两个端点 https://media.windows.net/
和 https://wamsbayclus001rest-hs.cloudapp.net/
但它在 Android 中不起作用。我相信 Android 正在改变 Headers 或者 headers 有问题导致 AMS 无法正确解析。谁能指导我如何使用 Android HttpUrlConnection
?
谢谢。
我通过将 OutputStream
更改为 DataOutputStream
DataOutputStream wrr = new DataOutputStream(urlConnection.getOutputStream());
wrr.write(this._postData);
wrr.flush();
wrr.close();
并将我的 json 数组更改为字符串,
String s = "{\"Name\":\"" + _fileName + ".mp4\", \"Options\":\"0\"}";
而且效果很好。