android 平台上的手机应用开发 how to user HttpUrlConnection POST Much Fields to Web Server ?
2010-03-09 17:37
453 查看
post.ashx
---------------------演示用文件
<%@ WebHandler Language="C#" Class="post" %>
using System;
using System.Web;
public class post : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Clear();
context.Response.Write(
string.Format("{0}:{1}", context.Request["firstName"], context.Request["secondname"]));
}
public bool IsReusable {
get {
return false;
}
}
}
----------------------------android 平台上的通过httpUrlConnection POST数据的方法:
@SuppressWarnings("unused")
private void postData2() {
String end = "/r/n";
String twoHyphens = "--";
String boundary = "*****";
try {
URL url = new URL("http://www.liusex.com/post.ashx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded" );
String urlParameters="firstname="+URLEncoder.encode("daihanzhang","UTF-8")
+"&secondname="+URLEncoder.encode("代汉章","UTF-8");
con.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(urlParameters);
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
mMessage.setText(b.toString());
ds.close();
} catch (Exception e) {
showDialog(e.getMessage());
}
}
@SuppressWarnings("unused")
private void postData3() {
String end = "/r/n";
String twoHyphens = "--";
String boundary = "---------------------------7d71a819230404";
try {
URL url = new URL("http://www.liusex.com/post.ashx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type","multipart/form-data; boundary="+boundary );
//---输出头
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; name=/"firstname/"");
ds.writeBytes("/r/n/r/n");
ds.writeBytes("daihanzhang");
ds.writeBytes(end);
ds.writeBytes(twoHyphens);
ds.writeBytes(boundary);
ds.writeBytes(end);
ds.writeBytes("Content-Disposition: form-data; name=/"secondname/"");
ds.writeBytes("/r/n/r/n");
ds.writeBytes("malijun");
ds.writeBytes(end);
ds.writeBytes(twoHyphens);
ds.writeBytes(boundary);
ds.writeBytes(end);
ds.writeBytes("Content-Disposition: form-data; name=/"thirdname/"");
ds.writeBytes("/r/n/r/n");
ds.writeBytes("jinqingqing");
//---输出尾巴
ds.writeBytes(end);
ds.writeBytes(twoHyphens);
ds.writeBytes(boundary);
ds.writeBytes(twoHyphens);
ds.writeBytes(end);
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
mMessage.setText(b.toString());
ds.close();
} catch (Exception e) {
showDialog(e.getMessage());
}
}
//此方法对于POST数据更为方便
@SuppressWarnings("unused")
private void postData1() {
HttpPost httppost = new HttpPost("http://www.liusex.com/post.ashx");
HttpClient httpclient = new DefaultHttpClient();
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("firstName", "12345"));
nameValuePairs.add(new BasicNameValuePair("secondname",
"android Name!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpclient.execute(httppost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
/* 读返回数据 */
String strResult = EntityUtils.toString(httpResponse
.getEntity());
mMessage.setText(strResult);
} else {
mMessage.setText("Error Response: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
---------------------演示用文件
<%@ WebHandler Language="C#" Class="post" %>
using System;
using System.Web;
public class post : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Clear();
context.Response.Write(
string.Format("{0}:{1}", context.Request["firstName"], context.Request["secondname"]));
}
public bool IsReusable {
get {
return false;
}
}
}
----------------------------android 平台上的通过httpUrlConnection POST数据的方法:
@SuppressWarnings("unused")
private void postData2() {
String end = "/r/n";
String twoHyphens = "--";
String boundary = "*****";
try {
URL url = new URL("http://www.liusex.com/post.ashx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded" );
String urlParameters="firstname="+URLEncoder.encode("daihanzhang","UTF-8")
+"&secondname="+URLEncoder.encode("代汉章","UTF-8");
con.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(urlParameters);
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
mMessage.setText(b.toString());
ds.close();
} catch (Exception e) {
showDialog(e.getMessage());
}
}
@SuppressWarnings("unused")
private void postData3() {
String end = "/r/n";
String twoHyphens = "--";
String boundary = "---------------------------7d71a819230404";
try {
URL url = new URL("http://www.liusex.com/post.ashx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type","multipart/form-data; boundary="+boundary );
//---输出头
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; name=/"firstname/"");
ds.writeBytes("/r/n/r/n");
ds.writeBytes("daihanzhang");
ds.writeBytes(end);
ds.writeBytes(twoHyphens);
ds.writeBytes(boundary);
ds.writeBytes(end);
ds.writeBytes("Content-Disposition: form-data; name=/"secondname/"");
ds.writeBytes("/r/n/r/n");
ds.writeBytes("malijun");
ds.writeBytes(end);
ds.writeBytes(twoHyphens);
ds.writeBytes(boundary);
ds.writeBytes(end);
ds.writeBytes("Content-Disposition: form-data; name=/"thirdname/"");
ds.writeBytes("/r/n/r/n");
ds.writeBytes("jinqingqing");
//---输出尾巴
ds.writeBytes(end);
ds.writeBytes(twoHyphens);
ds.writeBytes(boundary);
ds.writeBytes(twoHyphens);
ds.writeBytes(end);
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
mMessage.setText(b.toString());
ds.close();
} catch (Exception e) {
showDialog(e.getMessage());
}
}
//此方法对于POST数据更为方便
@SuppressWarnings("unused")
private void postData1() {
HttpPost httppost = new HttpPost("http://www.liusex.com/post.ashx");
HttpClient httpclient = new DefaultHttpClient();
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("firstName", "12345"));
nameValuePairs.add(new BasicNameValuePair("secondname",
"android Name!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpclient.execute(httppost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
/* 读返回数据 */
String strResult = EntityUtils.toString(httpResponse
.getEntity());
mMessage.setText(strResult);
} else {
mMessage.setText("Error Response: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
相关文章推荐
- Android平台的手机记账应用开发视频教程
- Android平台的手机记账应用开发全程实录
- 基于Android平台的手机记账应用开发全程实录
- android 平台上的手机应用开发 how to user HttpUrlConnection POST Much Fields to Web Server ?
- Android平台手机记账应用开发视频教程
- 索爱针对Android平台推出WebSDK, 主要是为其Android手机开发应用。
- 基于Android平台淘宝网手机客户端实战开发(共四季,涉及各类技术应用)
- Google手机操作系统Android应用开发入门
- Android 手机应用开发经验 之 通过Socket(TCP/IP)与PC通讯
- Android平台根据分辨率计算屏幕尺寸,基于物理尺寸来验证手机和平板应用合并的可行性
- Maemo Linux手机平台系列分析:(15) Maemo应用开发
- android手机视频应用开发
- Android开发把项目打包成apk,安卓到手机上,或者提交到app应用商店
- Android平台根据分辨率计算屏幕尺寸,基于物理尺寸来验证手机和平板应用合并的可行性
- Android开发_Android平台手机的优势与不足
- [转载]Android平台第三方应用分享到微信开发
- Android平台根据分辨率计算屏幕尺寸,基于物理尺寸来验证手机和平板应用合并的可行性
- 应用之星:领先的手机应用在线开发平台
- Microsoft Office 手机应用程式 即日起于 Android 及 IOS 平台免费下载!
- 实例详解快捷搭建Android手机开发平台