About the Java Post GET Request demo (Request C#iis Interface)

Source: Internet
Author: User

Nonsense not much to say directly on the code

 PackageDxq.httpgetdemo;ImportJava.io.ByteArrayOutputStream;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportJava.net.URLEncoder;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.app.Activity;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classMainactivityextendsActivityImplementsOnclicklistener {Button btn=NULL; TextView TV=NULL; PrivateHandler handler=NewHandler () { Public voidhandlemessage (Message mes) {if(mes!=NULL&&mes.what==0) {Tv.settext (mes.obj.toString ());    }        }    }; @Overrideprotected voidonCreate (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 voidOnClick (View arg0) {//TODO auto-generated Method Stub                Switch(Arg0.getid ()) { Caser.id.get:gethttp ();  Break;  Caser.id.post:posthttp ();  Break; }    }    /** GET Request interface **/     Public voidgethttp () {NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stub                Try{String Data= "Data=" +urlencoder.encode ("haha Get request Oh"); URL URL=NewURL ("Http://10.10.21.225:1106/Handler1.ashx"); HttpURLConnection Connection=(HttpURLConnection) url.openconnection (); Connection.setreadtimeout (5000); Connection.setrequestmethod ("GET");//Request method Note to capitalizeConnection.setconnecttimeout (5000); Connection.setrequestproperty ("Content-length", String.valueof (Data.length ())); Connection.setdooutput (true);//set True to write theConnection.getoutputstream (). Write (Data.getbytes ());//Write Data                    intCode=Connection.getresponsecode (); if(code==200) {InputStream is=Connection.getinputstream (); //Streamtools.readstream (IS);Bytearrayoutputstream bao=NewBytearrayoutputstream (); intLen=0; byte[] buffer=New byte[1024];  while(Len=is.read (buffer))!=-1) {bao.write (buffer,0, Len);                        } is.close (); Message mes=NewMessage (); Mes.what=0; Mes.obj=bao.tostring ();                        Handler.sendmessage (MES);                    Bao.close (); }Else{log.i ("Error Code", string.valueof (code)); }                                    } Catch(Exception e) {//TODO auto-generated Catch blockLOG.I ("Exception information", e.tostring ());                    }}). Start (); }        /** POST Request interface **/     Public voidposthttp () {NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubURL url; Try{String Data= "Data=" +urlencoder.encode ("hehe POST request"); URL=NewURL ("Http://10.10.21.225:1106/Handler1.ashx"); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setreadtimeout (1000); Conn.setrequestmethod ("POST");//POST request note to capitalizeConn.setconnecttimeout (2000); //Conn.setrequestproperty ("", NewValue)Conn.setdooutput (true);                    Conn.getoutputstream (). Write (Data.getbytes ()); intCode=Conn.getresponsecode (); if(code==200) {InputStream is=Conn.getinputstream (); //Streamtools.readstream (IS);Bytearrayoutputstream bao=NewBytearrayoutputstream (); intLen=0; byte[] buffer=New byte[1024];  while(Len=is.read (buffer))!=-1) {bao.write (buffer,0, Len);                        } is.close (); Message mes=NewMessage (); Mes.what=0; Mes.obj=bao.tostring ();                        Handler.sendmessage (MES);                                            Bao.close (); }Else{log.i ("Result Code", string.valueof (code)); }                                                        } Catch(Exception e) {//TODO auto-generated Catch blockLOG.I ("POST request Exception", e.tostring ());    }}). Start (); }
}

2. C #

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacewebapplication2{/// <summary>    ///Summary description of Handler1/// </summary>     Public classHandler1:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; if(Context. request["Data"] !=NULL) {context. Response.Write ("C # Interface"+context. request["Data"].                ToString ()); return; } context. Response.Write ("C # Interface"); }         Public BOOLisreusable {Get            {                return false; }        }    }}

3. Implementation results

4. Add a little don't forget the permissions

<android:name= "Android.permission.INTERNET"/>

About the Java Post GET Request demo (Request C#iis Interface)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.