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