標籤:ted tco [] onclick 請求 read java.net prot 圖片
廢話不多說,直接上代碼
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"/>
關於java post get請求Demo (請求c#iis介面)