Skip to content

Instantly share code, notes, and snippets.

@ezhov-da
Last active March 10, 2019 12:22
Show Gist options
  • Save ezhov-da/84469f9e0c37ecc40c306edc61868f8e to your computer and use it in GitHub Desktop.
Save ezhov-da/84469f9e0c37ecc40c306edc61868f8e to your computer and use it in GitHub Desktop.
java post изображения
[code:]java[:code]
package ru.ezhov.test.noui;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.swing.text.DefaultStyledDocument.ElementSpec.ContentType;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
*
* @author ezhov_da
*/
public class TestHttp
{
private static final Logger LOG = Logger.getLogger(TestHttp.class.getName());
public static void main(String[] args)
{
try
{
try (
DefaultHttpClient httpclient = new DefaultHttpClient();)
{
HttpHost proxy = new HttpHost("....");
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpPost httppost = new HttpPost("http://savepic.ru/");
//
httppost.setHeader("Host", "savepic.ru");
httppost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
httppost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httppost.setHeader("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");
httppost.setHeader("Accept-Encoding", "gzip, deflate");
httppost.setHeader("Referer", "http://savepic.ru/");
httppost.setHeader("Cookie", "PHPSESSID=pnur1a3iu0d8189cq1r4grq8s5");
httppost.setHeader("Connection", "keep-alive ");
//
FileBody bin = new FileBody(new File("E:\\~!until_you_delete!~\\wel_b_6.jpg"));
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("bin", bin)
.addPart("note", new StringBody(""))
.addPart("font1", new StringBody("decor"))
.addPart("font2", new StringBody("20"))
.addPart("orient", new StringBody("h"))
.addPart("size2", new StringBody("1024x768"))
.addPart("size1", new StringBody("1"))
.addPart("rotate", new StringBody("00"))
.addPart("flip", new StringBody("0"))
.addPart("mini", new StringBody("300x225"))
.addPart("opt3[]", new StringBody("zoom"))
.addPart("email", new StringBody(""))
.build();
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
try (CloseableHttpResponse response = httpclient.execute(httppost))
{
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null)
{
System.out.println("Response content length: " + resEntity.getContentLength());
InputStream inputStream = resEntity.getContent();
Scanner scanner = new Scanner(inputStream, "UTF-8");
while (scanner.hasNextLine())
{
System.out.println(scanner.nextLine());
}
}
EntityUtils.consume(resEntity);
}
}
} catch (MalformedURLException ex)
{
Logger.getLogger(TestHttp.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex)
{
Logger.getLogger(TestHttp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// URL url = new URL("http://savepic.ru/");
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("...", ...));
// HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
// Object object = httpURLConnection.getContent();
//
//
//
//
// Scanner scanner = new Scanner(httpURLConnection.getInputStream());
// while(scanner.hasNextLine()){
// System.out.println(scanner.nextLine());
// }
//
//
//
// httpURLConnection.disconnect();
[/code]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment