关于java post get请求Demo (请求c#iis接口)
废话不多说,直接上代码
package dxq.httpGetDemo; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { Button btn=null; TextView tv=null; private Handler handler=new Handler(){ public void handleMessage(Message mes){ if(mes!=null&&mes.what==0){ tv.setText(mes.obj.toString()); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.get); btn.setOnClickListener(this); btn=(Button)findViewById(R.id.post); btn.setOnClickListener(this); tv=(TextView)findViewById(R.id.content); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch(arg0.getId()){ case R.id.get: GetHttp(); break; case R.id.post: PostHttp(); break; } } /* * get请求接口 * */ public void GetHttp(){ new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub try { String data="data="+URLEncoder.encode("哈哈Get请求哦"); URL url=new URL("http://10.10.21.225:1106/Handler1.ashx"); HttpURLConnection connection=(HttpURLConnection)url.openConnection(); connection.setReadTimeout(5000); connection.setRequestMethod("GET"); //请求方式 注意要大写 connection.setConnectTimeout(5000); connection.setRequestProperty("Content-Length", String.valueOf(data.length())); connection.setDoOutput(true);//设置true 就可以写了 connection.getOutputStream().write(data.getBytes());//写入数据 int code=connection.getResponseCode(); if(code==200){ InputStream is=connection.getInputStream(); //StreamTools.ReadStream(is); ByteArrayOutputStream bao=new ByteArrayOutputStream(); int len=0; byte[] buffer=new byte[1024]; while((len=is.read(buffer))!=-1){ bao.write(buffer, 0, len); } is.close(); Message mes=new Message(); mes.what=0; mes.obj=bao.toString(); handler.sendMessage(mes); bao.close(); }else{ Log.i("错误码", String.valueOf(code)); } } catch (Exception e) { // TODO Auto-generated catch block Log.i("异常信息", e.toString()); } } }).start(); } /* * post请求接口 * */ public void PostHttp(){ new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub URL url; try { String data="data="+URLEncoder.encode("呵呵 post请求"); url = new URL("http://10.10.21.225:1106/Handler1.ashx"); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setReadTimeout(1000); conn.setRequestMethod("POST");//post请求 注意要大写 conn.setConnectTimeout(2000); //conn.setRequestProperty("", newValue) conn.setDoOutput(true); conn.getOutputStream().write(data.getBytes()); int code=conn.getResponseCode(); if(code==200){ InputStream is=conn.getInputStream(); //StreamTools.ReadStream(is); ByteArrayOutputStream bao=new ByteArrayOutputStream(); int len=0; byte[] buffer=new byte[1024]; while((len=is.read(buffer))!=-1){ bao.write(buffer, 0, len); } is.close(); Message mes=new Message(); mes.what=0; mes.obj=bao.toString(); handler.sendMessage(mes); bao.close(); }else{ Log.i("结果码", String.valueOf(code)); } } catch (Exception e) { // TODO Auto-generated catch block Log.i("post请求异常", e.toString()); } } }).start(); }
}
2. c#
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication2 { /// <summary> /// Handler1 的摘要说明 /// </summary> public class Handler1 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["data"] != null) { context.Response.Write("C#接口"+context.Request["data"].ToString()); return; } context.Response.Write("C#接口"); } public bool IsReusable { get { return false; } } } }
3. 执行结果
4. 增加一点 别忘了权限
<uses-permission android:name="android.permission.INTERNET"/>