发布于 2016-07-20 23:14:57 | 193 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java函数式编程,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


这篇文章主要介绍了简单介绍Java网络编程中的HTTP请求,需要的朋友可以参考下

HTTP请求的细节——请求行
 
  请求行中的GET称之为请求方式,请求方式有:POST、GET、HEAD、OPTIONS、DELETE、TRACE、PUT,常用的有: GET、 POST
  用户如果没有设置,默认情况下浏览器向服务器发送的都是get请求,例如在浏览器直接输地址访问,点超链接访问等都是get,用户如想把请求方式改为post,可通过更改表单的提交方式实现。
  不管POST或GET,都用于向服务器请求某个WEB资源,这两种方式的区别主要表现在数据传递上:如果请求方式为GET方式,则可以在请求的URL地址后以?的形式带上交给服务器的数据,多个数据之间以&进行分隔,例如:GET /mail/1.html?name=abc&password=xyz HTTP/1.1
  GET方式的特点:在URL地址后附带的参数是有限制的,其数据容量通常不能超过1K。
  如果请求方式为POST方式,则可以在请求的实体内容中向服务器发送数据,Post方式的特点:传送的数据量无限制。
 
HTTP请求的细节——消息头

 
  HTTP请求中的常用消息头
 
  accept:浏览器通过这个头告诉服务器,它所支持的数据类型
  Accept-Charset: 浏览器通过这个头告诉服务器,它支持哪种字符集
  Accept-Encoding:浏览器通过这个头告诉服务器,支持的压缩格式
  Accept-Language:浏览器通过这个头告诉服务器,它的语言环境
  Host:浏览器通过这个头告诉服务器,想访问哪台主机
  If-Modified-Since: 浏览器通过这个头告诉服务器,缓存数据的时间
  Referer:浏览器通过这个头告诉服务器,客户机是哪个页面来的  防盗链
  Connection:浏览器通过这个头告诉服务器,请求完后是断开链接还是何持链接

例:

http_get


import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
 
 
public class Http_Get { 
 
  private static String URL_PATH = "http://192.168.1.125:8080/myhttp/pro1.png"; 
 
  public Http_Get() { 
    // TODO Auto-generated constructor stub 
  } 
 
  public static void saveImageToDisk() { 
    InputStream inputStream = getInputStream(); 
    byte[] data = new byte[1024]; 
    int len = 0; 
    FileOutputStream fileOutputStream = null; 
    try { 
      fileOutputStream = new FileOutputStream("C:\\test.png"); 
      while ((len = inputStream.read(data)) != -1) { 
        fileOutputStream.write(data, 0, len); 
      } 
    } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } finally { 
      if (inputStream != null) { 
        try { 
          inputStream.close(); 
        } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
        } 
      } 
      if (fileOutputStream != null) { 
        try { 
          fileOutputStream.close(); 
        } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
        } 
      } 
    } 
  } 
 
  /** 
   * 获得服务器端的数据,以InputStream形式返回 
   * @return 
   */ 
  public static InputStream getInputStream() { 
    InputStream inputStream = null; 
    HttpURLConnection httpURLConnection = null; 
    try { 
      URL url = new URL(URL_PATH); 
      if (url != null) { 
        httpURLConnection = (HttpURLConnection) url.openConnection(); 
        // 设置连接网络的超时时间 
        httpURLConnection.setConnectTimeout(3000); 
        httpURLConnection.setDoInput(true); 
        // 表示设置本次http请求使用GET方式请求 
        httpURLConnection.setRequestMethod("GET"); 
        int responseCode = httpURLConnection.getResponseCode(); 
        if (responseCode == 200) { 
          // 从服务器获得一个输入流 
          inputStream = httpURLConnection.getInputStream(); 
        } 
      } 
    } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
    return inputStream; 
  } 
 
  public static void main(String[] args) { 
    // 从服务器获得图片保存到本地 
    saveImageToDisk(); 
  } 
} 

Http_Post


import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.HashMap; 
import java.util.Map; 
 
public class Http_Post { 
 
  // 请求服务器端的url 
  private static String PATH = "http://192.168.1.125:8080/myhttp/servlet/LoginAction"; 
  private static URL url; 
 
  public Http_Post() { 
    // TODO Auto-generated constructor stub 
  } 
 
  static { 
    try { 
      url = new URL(PATH); 
    } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
  } 
 
  /** 
   * @param params 
   *      填写的url的参数 
   * @param encode 
   *      字节编码 
   * @return 
   */ 
  public static String sendPostMessage(Map<String, String> params, 
      String encode) { 
    // 作为StringBuffer初始化的字符串 
    StringBuffer buffer = new StringBuffer(); 
    try { 
      if (params != null && !params.isEmpty()) { 
         for (Map.Entry<String, String> entry : params.entrySet()) { 
            // 完成转码操作 
            buffer.append(entry.getKey()).append("=").append( 
                URLEncoder.encode(entry.getValue(), encode)) 
                .append("&"); 
          } 
        buffer.deleteCharAt(buffer.length() - 1); 
      } 
      // System.out.println(buffer.toString()); 
      // 删除掉最有一个& 
       
      System.out.println("-->>"+buffer.toString()); 
      HttpURLConnection urlConnection = (HttpURLConnection) url 
          .openConnection(); 
      urlConnection.setConnectTimeout(3000); 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.setDoInput(true);// 表示从服务器获取数据 
      urlConnection.setDoOutput(true);// 表示向服务器写数据 
      // 获得上传信息的字节大小以及长度 
      byte[] mydata = buffer.toString().getBytes(); 
      // 表示设置请求体的类型是文本类型 
      urlConnection.setRequestProperty("Content-Type", 
          "application/x-www-form-urlencoded"); 
      urlConnection.setRequestProperty("Content-Length", 
          String.valueOf(mydata.length)); 
      // 获得输出流,向服务器输出数据 
      OutputStream outputStream = urlConnection.getOutputStream(); 
      outputStream.write(mydata,0,mydata.length); 
      outputStream.close(); 
      // 获得服务器响应的结果和状态码 
      int responseCode = urlConnection.getResponseCode(); 
      if (responseCode == 200) { 
        return changeInputStream(urlConnection.getInputStream(), encode); 
      } 
    } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
    return ""; 
  } 
 
  /** 
   * 将一个输入流转换成指定编码的字符串 
   * 
   * @param inputStream 
   * @param encode 
   * @return 
   */ 
  private static String changeInputStream(InputStream inputStream, 
      String encode) { 
    // TODO Auto-generated method stub 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    byte[] data = new byte[1024]; 
    int len = 0; 
    String result = ""; 
    if (inputStream != null) { 
      try { 
        while ((len = inputStream.read(data)) != -1) { 
          outputStream.write(data, 0, len); 
        } 
        result = new String(outputStream.toByteArray(), encode); 
      } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
    } 
    return result; 
  } 
 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Map<String, String> params = new HashMap<String, String>(); 
    params.put("username", "admin"); 
    params.put("password", "123"); 
    String result = Http_Post.sendPostMessage(params, "utf-8"); 
    System.out.println("--result->>" + result); 
  } 
 
} 




最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务