|
| 1 | +package com.janekey.httpserver.test; |
| 2 | + |
| 3 | +import com.janekey.httpserver.util.StringUtil; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.OutputStream; |
| 8 | +import java.io.OutputStreamWriter; |
| 9 | +import java.net.HttpURLConnection; |
| 10 | +import java.net.URL; |
| 11 | +import java.nio.ByteBuffer; |
| 12 | + |
| 13 | +/** |
| 14 | + * User: janekey |
| 15 | + * Date: 14-11-20 |
| 16 | + * Time: 下午6:34 |
| 17 | + */ |
| 18 | +public class HttpClient { |
| 19 | + private HttpURLConnection connection; |
| 20 | + private static final String BOUNDARY = "---------------------------7db15a14291cce"; |
| 21 | + private String lineSeparator; |
| 22 | + private static final byte[] EMPTY_BUFFER = new byte[0]; |
| 23 | + |
| 24 | + public HttpClient(String url) throws IOException { |
| 25 | + URL u = new URL(url); |
| 26 | + connection = (HttpURLConnection) u.openConnection(); |
| 27 | +// lineSeparator = java.security.AccessController.doPrivileged( |
| 28 | +// new sun.security.action.GetPropertyAction("line.separator")); |
| 29 | + lineSeparator = "\r\n"; |
| 30 | + } |
| 31 | + |
| 32 | + private void initHead() { |
| 33 | +// connection.setRequestProperty("Accept", accept); |
| 34 | +// connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); |
| 35 | +// if (referer != null) connection.setRequestProperty("Referer", referer); |
| 36 | +// connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); |
| 37 | +// connection.setRequestProperty("X-Requested-With", "XMLHttpRequest"); |
| 38 | + } |
| 39 | + |
| 40 | + private void initMultipartHead() { |
| 41 | + connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); |
| 42 | + } |
| 43 | + |
| 44 | + private void writeData(String data) throws IOException { |
| 45 | + OutputStream outputStream = connection.getOutputStream(); |
| 46 | + OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); |
| 47 | + writer.write(data); |
| 48 | + writer.close(); |
| 49 | + } |
| 50 | + |
| 51 | + private void writeData(byte[] buffer) throws IOException { |
| 52 | + OutputStream outputStream = connection.getOutputStream(); |
| 53 | + outputStream.write(buffer); |
| 54 | + outputStream.close(); |
| 55 | + } |
| 56 | + |
| 57 | + private String readData() throws IOException { |
| 58 | + InputStream is = connection.getInputStream(); |
| 59 | + int available = is.available(); |
| 60 | + byte[] buffer = new byte[available]; |
| 61 | + String bufferString = null; |
| 62 | + if (is.read(buffer) != -1) { |
| 63 | + bufferString = new String(buffer); |
| 64 | + } |
| 65 | + is.close(); |
| 66 | + return bufferString; |
| 67 | + } |
| 68 | + |
| 69 | + private byte[] readOriginalData() throws IOException { |
| 70 | + InputStream is = connection.getInputStream(); |
| 71 | + int available = is.available(); |
| 72 | + byte[] buffer = new byte[available]; |
| 73 | + if (is.read(buffer) != -1) { |
| 74 | + return buffer; |
| 75 | + } else { |
| 76 | + return EMPTY_BUFFER; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public String post(String data) throws IOException { |
| 81 | + initHead(); |
| 82 | + |
| 83 | + connection.setDoOutput(true); |
| 84 | + connection.setRequestMethod("POST"); |
| 85 | + |
| 86 | + if (data != null) { |
| 87 | + writeData(data); |
| 88 | + } |
| 89 | + return readData(); |
| 90 | + } |
| 91 | + |
| 92 | + public String get() throws IOException { |
| 93 | + initHead(); |
| 94 | + |
| 95 | + connection.setDoOutput(false); |
| 96 | + connection.setRequestMethod("GET"); |
| 97 | + |
| 98 | + return readData(); |
| 99 | + } |
| 100 | + |
| 101 | + public byte[] getOriginal() throws IOException { |
| 102 | + initHead(); |
| 103 | + |
| 104 | + connection.setDoOutput(false); |
| 105 | + connection.setRequestMethod("GET"); |
| 106 | + |
| 107 | + return readOriginalData(); |
| 108 | + } |
| 109 | + |
| 110 | + public String multipart(String name, String fileName, byte[] data) throws IOException { |
| 111 | + initMultipartHead(); |
| 112 | + |
| 113 | + connection.setDoOutput(true); |
| 114 | + connection.setRequestMethod("POST"); |
| 115 | + |
| 116 | + //multipart/form-data body buffer |
| 117 | + ByteBuffer buffer = ByteBuffer.allocate(data.length + 200); |
| 118 | + |
| 119 | + StringBuilder string = new StringBuilder(); |
| 120 | + string.append("--").append(BOUNDARY).append(lineSeparator); |
| 121 | + string.append("Content-Disposition: form-data; name=\"").append(name).append("\"; filename=\"") |
| 122 | + .append(fileName).append("\"").append(lineSeparator); |
| 123 | + string.append("Content-Type: image/jpeg").append(lineSeparator).append(lineSeparator); |
| 124 | + |
| 125 | + buffer.put(StringUtil.getUTF8Bytes(string.toString())); |
| 126 | + buffer.put(data); |
| 127 | + |
| 128 | + string.delete(0, string.length()); |
| 129 | + string.append(lineSeparator).append("--").append(BOUNDARY).append("--").append(lineSeparator); |
| 130 | + buffer.put(StringUtil.getUTF8Bytes(string.toString())); |
| 131 | + writeData(buffer.array()); |
| 132 | + return readData(); |
| 133 | + } |
| 134 | +} |
0 commit comments