java通过post调用ashx_关于java post get请求Demo (请求c#iis接口)

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 class MainActivity extends Activity implementsOnClickListener {

Button btn=null;

TextView tv=null;private Handler 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);

}

@Overridepublic voidonClick(View arg0) {//TODO Auto-generated method stub

switch(arg0.getId()){caseR.id.get:

GetHttp();break;caseR.id.post:

PostHttp();break;

}

}/** get请求接口

**/

public voidGetHttp(){new Thread(newRunnable(){

@Overridepublic voidrun() {//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=newByteArrayOutputStream();int len=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("错误码", String.valueOf(code));

}

}catch(Exception e) {//TODO Auto-generated catch block

Log.i("异常信息", e.toString());

}

}

}).start();

}/** post请求接口

**/

public voidPostHttp(){new Thread(newRunnable(){

@Overridepublic voidrun() {//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=newByteArrayOutputStream();int len=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("结果码", String.valueOf(code));

}

}catch(Exception e) {//TODO Auto-generated catch block

Log.i("post请求异常", e.toString());

}

}

}).start();

}

}

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页