This post demonstarte client\server interaction.
The client is android app and the server is asp.net based.
Client
The client use HttpClient , HttpPost and UrlEncodedFormEntity to create form based request to an asp based server.
The form has two pairs :
- key - "userName" , value - "Nathan"
- key - "userPassword" , value - "p456"
HttpClient client1 = new DefaultHttpClient(); HttpPost request = new HttpPost(urlText.getText().toString()); String user = "userName",pass = "userPassword"; List<NameValuePair> postParameters = new ArrayList<NameValuePair>(2); postParameters.add(new BasicNameValuePair(user, "Nathan")); postParameters.add(new BasicNameValuePair(pass, "p456")); try { request.setEntity(new UrlEncodedFormEntity(postParameters)); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); request.setEntity(formEntity); HttpResponse response; response = client1.execute(request);
The server response is processed as followes
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line; String page = ""; line = in.readLine(); while (line != null) { page = page + line; line = in.readLine(); } textViewMessage.setText(page);
The response is actually the html file filled with user name and password as described in asp file
Server
The server is a simple IIS as described in HTML based Web Site (chap 2 web beginners) but an asp file is used.
The asp file access the keys userName, userPassword and return html as response
Source sample
HttpPost.zip
Run the application to create :
Click get to create
Note that the server response is the html file as described in second.aspx but the value of userName and userPassword which were sent by the client are used
remarks :
- the host ip is not shown.
- in case the host is your local computer then you will have to disable the firewall
Nathan
I really like this all information about Android - Connection to the internet 2. It is very useful for software developer. So thanks for share this post.
ReplyDeleteupc code