關於java post get請求Demo (請求c#iis介面)

來源:互聯網
上載者:User

標籤: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介面)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.