
import java.net.Socket;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.io.*;
public class HttpConnection {
Socket _socket;
public HttpConnection(String host, int port) throws IOException {
InetAddress addr = InetAddress.getByName(host);
_socket = new Socket(addr, port);
}
public static void main(String[] args) {
try {
HttpConnection conn = new HttpConnection("music-box", 8083);
HttpConnection.Parameter param = new
HttpConnection.Parameter("subscriber","i");
//POST 1
String output = conn.post("/login", new HttpConnection.Parameter[]{param});
System.out.println(output);
//POST 2
output = conn.post("/logoff", new HttpConnection.Parameter[]{param});
System.out.println(output);
conn.close();
} catch (Exception e) {
}
}
public void close(){
if(_socket != null) {
try {
_socket.close();
}
catch (IOException ex) {
}
}
}
public String post(String servlet, Parameter[] params) throws IOException {
String data = getDataString(params);
String path = servlet;
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(_socket.getOutputStream(), "UTF8"));
wr.write("POST " + path + " HTTP/1.0\r\n");
wr.write("Content-Length: " + data.length() + "\r\n");
wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
wr.write("\r\n");
// Send data
wr.write(data);
wr.flush();
// Get response
String line;
StringBuffer buf = new StringBuffer();
BufferedReader rd = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
while ((line = rd.readLine()) != null) {
// Process line...
buf.append(line);
}
return buf.toString();
}
public String get(String servlet) throws IOException {
String path = servlet;
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(_socket.getOutputStream(), "UTF8"));
wr.write("GET " + path + " HTTP/1.0\r\n");
wr.flush();
// Get response
String line;
StringBuffer buf = new StringBuffer();
BufferedReader rd = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
while ((line = rd.readLine()) != null) {
// Process line...
buf.append(line);
}
return buf.toString();
}
private String getDataString(Parameter[] params){
StringBuffer dataBuf = new StringBuffer();
for(int i = 0; i < params.length; i++) {
if(i != 0) {
dataBuf.append("&");
}
dataBuf.append(params[i].toString());
}
return dataBuf.toString();
}
public static class Parameter {
public String name;
public String value;
public Parameter(String name, String value){
this.name = name;
this.value = value;
}
public String toString() {
try {
return URLEncoder.encode(name, "UTF-8") + "=" +
URLEncoder.encode(value, "UTF-8");
}
catch(UnsupportedEncodingException ex) {
return null;
}
}
}
}
private static Collection getSubstringsThatMatchesPattern(String source, String pattern) {
Collection result = new TreeSet();
Matcher matcher = Pattern.compile(pattern).matcher(source);
while(matcher.find()) {
result.add(matcher.group());
}
return result;
}
Проходила конференция в гостинице Европа. Очень понравилась еда. Кадр с места событий:
Жадный Oracle всех купил.
Создаем функцию конвертирования:
public static void createFile(InputStream io, String fileName) throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
byte[] buf = new byte[256];
int read = 0;
while ((read = io.read(buf)) > 0) {
fos.write(buf, 0, read);
}
}Используем:
response.setContentType("text/xml");
URL url = new URL("ftp://user:password@FTP.address.COM/file.xml");
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream(); // to download
createFile(is, "file1.xml");


![]() | You are viewing Log in Create a LiveJournal Account Learn more | Explore LJ: Life Entertainment Music Culture News & Politics Technology |