Skip to content

Instantly share code, notes, and snippets.

@mouseroot
Created August 23, 2012 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mouseroot/3431352 to your computer and use it in GitHub Desktop.
Save mouseroot/3431352 to your computer and use it in GitHub Desktop.
Post requests in java
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Requests {
static String domain;
static URL myurl;
static URLConnection myurlc;
static OutputStreamWriter osr;
static BufferedReader br;
static String line;
static String resp;
static StringBuilder sb;
public Requests(String d) {
domain = d;
}
public String sendPost(String data) {
try {
sb = new StringBuilder();
myurl = new URL(domain);
myurlc = myurl.openConnection();
myurlc.setDoOutput(true);
osr = new OutputStreamWriter(myurlc.getOutputStream());
osr.write(data);
osr.flush();
br = new BufferedReader(new InputStreamReader(myurlc.getInputStream()));
while((line = br.readLine()) != null)
{
sb.append(line);
}
br.close();
osr.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null,"IO Error");
}
finally {
return sb.toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment