zoukankan      html  css  js  c++  java
  • Android通过HTTP POST带參訪问asp.net网页

    在看了网络上非常多视频关于android通过HTTP POST或者GET方式訪问网页并获取数据的方法。

    自己也copy了一份来測试。并通过C#.NET搭建了一个简单的后台,但发现传參时,依照网上的方式来做无法得到对应的结果。

    下面是我的求贴

    http://bbs.csdn.net/topics/390814679

    发了好久都没有人关于答复这个问题,预计大家都不是使用ASP.NET来做后台。

    经过了重复的測试手机端代码。发现事实上ASP.NET做的后台,事实上可以直接解析URL中带參数,不须要通过网上介绍的方法实现

    下面是截取測试代码的主要部分:

    button触发:

    <span style="white-space:pre">	</span>final Button btn2 = (Button) findViewById(R.id.button2);
    		btn2.setOnClickListener(new OnClickListener() {
    			public void onClick(View v) {
    				
    
    				progressDialog = ProgressDialog.show(MainActivity.this,
    						"载入中...", "请等待...", true, false);
    
    				// 新建线程
    				new Thread() {
    
    					@Override
    					public void run() {
    						// 须要花时间计算的方法
    						try {
    											
    							String str = posturl("http://aspspider.info/lanjackg2003/Default.aspx?name=lan120576664&psw=456");							
    							textViewhttpRes.setText(str.toString());
    							
    						} catch (Exception e) {
    							// TODO: handle exception
    						}
    
    						// 向handler发消息
    						handler.sendEmptyMessage(0);
    					}
    				}.start();
    
    			}
    
    		});
    获取网页数据的代码:

     public String posturl(String url){
            InputStream is = null;
            String result = "";
    
            try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            }catch(Exception e){
                return "Fail to establish http connection!"+e.toString();
            }
    
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "
    ");
                }
                is.close();
    
                result=sb.toString();
                Log.v(LOG_TAG,result.toString());
            }catch(Exception e){
                return "Fail to convert net stream!";
            }
    
            return result;
        }
    

    手机显示

    PC端显示显示:


    PC与手机显示的结果是一致的。


  • 相关阅读:
    Network (poj1144)
    C. Hongcow Builds A Nation
    ZYB loves Xor I(hud5269)
    D. Chloe and pleasant prizes
    Game(hdu5218)
    约瑟夫环的递推方法
    Misaki's Kiss again(hdu5175)
    Exploration(hdu5222)
    B. Arpa's weak amphitheater and Mehrdad's valuable Hoses
    C. Arpa's loud Owf and Mehrdad's evil plan
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5221570.html
Copyright © 2011-2022 走看看