How to extract data at the server end?
I have used the following code on my c开发者_如何转开发lient side (Android emulator).
HttpClient client=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://117.192.xxx.xxx:8080/System_Alpha/");
List pairs=new ArrayList();
String strUsername=username.getText().toString();
String strPassword=password.getText().toString();
pairs.add(new BasicNameValuePair("username", strUsername));
pairs.add(new BasicNameValuePair("password", strPassword));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response= client.execute(httpPost);
My question is, at the server end, how can I extract these values? That is, the username and password?
That depends on what you have on the server side.
Say: if you have a java servlet, you just retrieve the necessary data in your doGet
(resp. doPost
) method:
final String userName = (String) request.getParameter("userName");
final String password = (String) request.getParameter("password");
Update 1
Here is an example of a simple servlet that authenticates a user with the two parameters above, and sends back an xml response:
public class LoginServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/xml");
final ServletOutputStream out = response.getOutputStream();
try
{
final String userName = (String) request.getParameter("userName");
final String password = (String) request.getParameter("password");
final boolean authenticated = login(userName, password);
if (authenticated)
out.println("<login><status>SUCCESS</status></login>");
else
out.println("<login><status>FAIL</status></login>");
}
catch (Exception e)
{
out.println("<login><status>ERROR</status></login>");
}
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doGet(request, response);
}
private boolean login(final String userName, final String password)
{
// TODO: authenticate the user and return the result: true/false
}
}
In your web.xml
you declare your servlet by writing:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>[yourpackagename].LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet/*</url-pattern>
</servlet-mapping>
where [yourpackagename]
is the package where your LoginServlet
is declared.
This way, if all these are in a web application named Try
, and you deploy it, you can access your login servlet over http at http://localhost:8080/Try/LoginServlet/
.
Update 2
To retrieve the data from the HttpResponse, you need:
HttpEntity entity = response.getEntity();
final InputStream inputStream = entity.getContent();
You handle this InputStream
depending on what kind of data it contains.
If you need the String
value of the response entity:
HttpEntity entity = response.getEntity();
final String responseText = EntityUtils.toString(entity);
精彩评论