Created
August 23, 2012 02:11
-
-
Save mouseroot/3431352 to your computer and use it in GitHub Desktop.
Post requests in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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