首先,我必须说我是HTTP的新手。我正试图在我的父母那里提交一个帖子请求'餐厅网站,以便从我的应用程序注册新用户。
网站是这样的(西班牙文):http://pedidos.pizzeriabritannia.com/index.asp?Opc=Registro
据我所知,帖子请求有这些valuePairs:
<form name="formulario" method="post" action="index.asp?Opc=Registro&Sub=Nuevo" >
<input type="text" size="20" name="nombre" value="" style="background-color:#f5f7fe" />
<input type="text" size="30" name="apellidos" value="" style="background-color:#f5f7fe" />
<input type="text" size="30" name="direccion" value="" style="background-color:#f5f7fe" />
<input type="text" size="20" name="nif" id="nif" style="background-color:#f5f7fe" value="" />
<input type="text" size="30" name="email" id="email" value="" style="background-color:#f5f7fe"/>
<input type="text" size="20" name="telefono" value="" style="background-color:#f5f7fe" />
<input type="text" size="10" name="clave" value="" style="background-color:#f5f7fe" />
<input type="checkbox" value="S" name="correo" checked="checked" />
在我的Android代码中,我这样做:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://pedidos.pizzeriabritannia.com/index.asp?Opc=Registrado");
StringBuilder data = null;
String answer ="";
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("nombre", Nombre));
nameValuePairs.add(new BasicNameValuePair("apellidos", Apellidos));
nameValuePairs.add(new BasicNameValuePair("calle", Calle));
nameValuePairs.add(new BasicNameValuePair("direccion", Detalles));
nameValuePairs.add(new BasicNameValuePair("email", Email));
nameValuePairs.add(new BasicNameValuePair("clave", contrasena));
nameValuePairs.add(new BasicNameValuePair("telefono", telefono));
nameValuePairs.add(new BasicNameValuePair("nif", ""));
nameValuePairs.add(new BasicNameValuePair("correo", "s"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
data = inputStreamToString(response.getEntity().getContent());
int code = response.getStatusLine().getStatusCode();
answer = code + data.toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return answer;
}
我得到200作为回复,这意味着一切都很顺利,但问题是应该将电子邮件发送到表单中提供的电子邮件地址,但这不会发生。 你有什么主意吗?我可以遵循任何建议吗?
EDITTED:
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Return full string
return total;
}
答案 0 :(得分:1)
HTML摘录中提到了以下地址:index.asp?Opc=Registro&Sub=Nuevo
您显然是在index.asp?Opc=Registrado
上提出请求,这是不同的,因为&Sub=Nuevo
部分缺失。 (Nuevo值可能表示创建了新项目。)
答案 1 :(得分:0)
data = inputStreamToString(response.getEntity().getContent());
替换为
data = EntityUtils.toString(response.getEntity());