zoukankan      html  css  js  c++  java
  • Http 范例

    package com.android.test;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;

    public class HelloActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           
           
            //sendHttp();
            sendRequest("56.240.128.148",8045,"/dfsdf/login","user=admin&pwd=1");
        }
        private void sendHttp(){
         String httpUrl = "http://56.240.128.148:8045/dfsdf/login";
         HttpPost httpRequest = new HttpPost(httpUrl);  
         List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add(new BasicNameValuePair("user", "admin"));  
      params.add(new BasicNameValuePair("pwd", "1"));      
      try{   
                //设置字符集   
               HttpEntity httpentity = new UrlEncodedFormEntity(params, "GB2312");//"gb2312");   
               //请求httpRequest   
               httpRequest.setEntity(httpentity);   
               //取得默认的HttpClient   
               HttpClient httpclient = new DefaultHttpClient();   
                 //取得HttpResponse   
                 HttpResponse httpResponse = httpclient.execute(httpRequest);   
                 //HttpStatus.SC_OK表示连接成功   
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){   
                     //取得返回的字符串   
                     String strResult = EntityUtils.toString(httpResponse.getEntity());   
                     Log.v("jkj","fsdfsdfsd"); 
                 }else{   
                  Log.v("jkj","fsdfsdfsd");
                }   
             }catch (ClientProtocolException e){   
                // resultText.setText(e.getMessage().toString());   
             } catch (IOException e){   
               //  resultText.setText(e.getMessage().toString());   
             }catch (Exception e){   
              Log.v("jkj","fsdfsdfsd"); 
             } 
        }
       
        /////////////////////////////////////////////////////////////////////
       
        private void sendRequest(String hostIp,int port,String cmd,String data){
         Socket socket;   
            try {   
                socket = new Socket(hostIp, port);   
                OutputStream os = socket.getOutputStream();   
               // String data= getXmlString();   
                StringBuffer sb = new StringBuffer();   
                sb.append("POST "+cmd+" HTTP/1.1\r\n");// 注意\r\n为回车换行   
               // sb.append("Accept-Language: zh-cn\r\n");   
               // sb.append("Connection: Keep-Alive\r\n");   
               // sb.append("Host:localhost\r\n");   
               // sb.append("Content-Length:11\r\n");
                sb.append("Host: "+hostIp+":"+port+"\r\n");
                sb.append("Content-Type: application/x-www-form-urlencoded\r\n");
                sb.append("Content-MD5: a781asdfadfbadsf1apoqst\r\n");//??????
                sb.append("Accept-Encoding: gzip,deflate\r\n");
                sb.append("Accept-Charset: GB2312\r\n");
                sb.append("Keep-Alive: 115\r\n");
                sb.append("\r\n");   
                sb.append(data+"\r\n");   
                //sb.append("\r\n");   
      
      
                // 接收Web服务器返回HTTP响应包   
                os.write(sb.toString().getBytes());   
                os.flush();   
               
                InputStream ins = socket.getInputStream();
                byte[] b = new byte[1000];   
                ins.read(b); //读取http头   
                InputStreamReader ireader = new InputStreamReader(ins);   
                java.io.BufferedReader reader = new java.io.BufferedReader(ireader);  
                String s =  reader.readLine(); //读取内容   
                System.out.println("response:"+s);   
                System.out.println(reader.readLine());   
                System.out.println(reader.readLine());   
                System.out.println(reader.readLine());   
                System.out.println(reader.readLine());   
                System.out.println(reader.readLine());   
                System.out.println(reader.readLine());   
                   
            } catch (UnknownHostException e) {   
                e.printStackTrace();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }
         
        }
    }

  • 相关阅读:
    CSDN的验证码,真得很糟糕
    CSDN的验证码,为什么要这样呢
    <转>http协议 文件下载原理详解
    验证码,验证码,继续
    搞了一天,气死我了
    昨天下午三点,到晚上六点写的(干死单文档)
    一个早晨加,一个上午的结果
    Linux+QT4+我忙活半宿的结果
    Linux真好玩阿,不过我家电脑不行,运行不够流畅
    springboot之整合JPA
  • 原文地址:https://www.cnblogs.com/leaven/p/2614017.html
Copyright © 2011-2022 走看看