Android 无法 POST /api 响应
Android Cannot POST /api response
我正在尝试在 REST 后端执行 HTTP post 请求。后端的 URL 使用 SSL,因此我还添加了必要的代码来处理它。但我得到了以下回复:
Cannot POST /api
这是我的代码:
protected String doInBackground(String... args) {
try {
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
HttpPost post = new HttpPost("https://test-api.smart-trial.dk/api");
List <NameValuePair> nvps = new ArrayList <NameValuePair>(2);
nvps.add(new BasicNameValuePair("path[pathwayId]","5566e151817a62021b1ea809"));
nvps.add(new BasicNameValuePair("formData[firstname]","Name"));
nvps.add(new BasicNameValuePair("formData[lastname]","XXX"));
nvps.add(new BasicNameValuePair("formData[email]","example@mail.com"));
post.addHeader("Referer" ,"https://myurl.com/api");
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
//DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null) {
// get the response content as a string
String stringResponse = EntityUtils.toString(entity);
Log.d("Response", stringResponse);
}
代码有什么问题吗?或者至少为什么我会得到这样的回应。
编辑
我对参数也有一些规则。
"The post should be done with x-www-form-urlencoded body parameters"
I think more or less I over that by using post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
which x-www-form-urlencoded.
参数:
- pathwayId(应位于路径中)
- 名字(应位于 formData 中)
- 姓氏(应位于 formData 中)
- 电子邮件(应位于 formData 中)
- 推荐人(应位于Header)
听起来您的服务器未配置为允许 POST 请求 URL。但是你需要一种方法来验证这一点。
如果您还没有适用于浏览器的 REST 测试插件,请找到一个允许您输入 POST 请求数据的插件,下载并安装它。
然后复制浏览器插件中的POST数据,提交请求,查看服务器响应。
至少这应该可以帮助您确定问题出在应用程序中还是服务器中。
我正在尝试在 REST 后端执行 HTTP post 请求。后端的 URL 使用 SSL,因此我还添加了必要的代码来处理它。但我得到了以下回复:
Cannot POST /api
这是我的代码:
protected String doInBackground(String... args) {
try {
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
HttpPost post = new HttpPost("https://test-api.smart-trial.dk/api");
List <NameValuePair> nvps = new ArrayList <NameValuePair>(2);
nvps.add(new BasicNameValuePair("path[pathwayId]","5566e151817a62021b1ea809"));
nvps.add(new BasicNameValuePair("formData[firstname]","Name"));
nvps.add(new BasicNameValuePair("formData[lastname]","XXX"));
nvps.add(new BasicNameValuePair("formData[email]","example@mail.com"));
post.addHeader("Referer" ,"https://myurl.com/api");
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
//DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null) {
// get the response content as a string
String stringResponse = EntityUtils.toString(entity);
Log.d("Response", stringResponse);
}
代码有什么问题吗?或者至少为什么我会得到这样的回应。
编辑
我对参数也有一些规则。
"The post should be done with x-www-form-urlencoded body parameters"
I think more or less I over that by usingpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
which x-www-form-urlencoded.
参数:
- pathwayId(应位于路径中)
- 名字(应位于 formData 中)
- 姓氏(应位于 formData 中)
- 电子邮件(应位于 formData 中)
- 推荐人(应位于Header)
听起来您的服务器未配置为允许 POST 请求 URL。但是你需要一种方法来验证这一点。
如果您还没有适用于浏览器的 REST 测试插件,请找到一个允许您输入 POST 请求数据的插件,下载并安装它。
然后复制浏览器插件中的POST数据,提交请求,查看服务器响应。
至少这应该可以帮助您确定问题出在应用程序中还是服务器中。