您的位置:首页 > 理论基础 > 计算机网络

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: