Java HTTP basic framework

Source: Internet
Author: User


Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import java.net.HttpURLConnection;
Import java.net.InetSocketAddress;
Import Java.net.Proxy;
Import Java.net.URL;

public class Loginutil {

public static string Get (String url,string key) {
{
String proxyhost= "";
int proxyport=111;
Boolean isproxy=false;
String result = "";
BufferedReader in = null;
try {
URL realurl = new URL (URL);
HttpURLConnection conn = null;
if (IsProxy) {
Using proxy IP
@SuppressWarnings ("Static-access")
Proxy proxy = new Proxy (Proxy.Type.DIRECT.HTTP, new Inetsocketaddress (ProxyHost, ProxyPort));
conn = (httpurlconnection) realurl.openconnection (proxy);//Use proxy IP, open and connect between URLs
}else {
Do not use proxy IP
conn = (httpurlconnection) realurl.openconnection ()//Do not use proxy IP, open and connect between URLs
}
To send a POST request, you must set the following two lines
Conn.setdooutput (TRUE);
Conn.setdoinput (TRUE);
To set common request properties
Conn.setconnecttimeout (2 * 1000);
Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
Conn.setrequestproperty ("Referer", Referer);
Conn.setrequestproperty ("Key", key);
Establish a real HTTP connection
Conn.connect ();
Defines the response of the BufferedReader input stream to read the URL
in = new BufferedReader (New InputStreamReader (Conn.getinputstream (), "Utf-8"));
String Line;
while (line = In.readline ()) = null) {
result = result + line + "\ r \ n";
}
} catch (Exception e) {
System.out.println ("Send GET Request exception!") "+ e);
E.printstacktrace ();
} finally {
try{
if (in!=null) {
In.close ();
}
}
catch (IOException ex) {
Ex.printstacktrace ();
}
}
return result;
}
}
public static string Sendgift (string URL, string param,string key) {
String result = "";
int proxyport=111;
String proxyhost= "1.1.1.1";
Boolean isproxy=false;
OutputStreamWriter out = null;
BufferedReader in = null;
try {
URL realurl = new URL (URL);
HttpURLConnection conn = null;
if (IsProxy) {
Using proxy IP
@SuppressWarnings ("Static-access")
Proxy proxy = new Proxy (Proxy.Type.DIRECT.HTTP, new Inetsocketaddress (ProxyHost, ProxyPort));
conn = (httpurlconnection) realurl.openconnection (proxy);//Use proxy IP, open and connect between URLs
}else {
Do not use proxy IP
conn = (httpurlconnection) realurl.openconnection ()//Do not use proxy IP, open and connect between URLs
}
To send a POST request, you must set the following two lines
Conn.setdooutput (TRUE);
Conn.setdoinput (TRUE);
Post method
Conn.setrequestmethod ("POST");
To set common request properties
Conn.setconnecttimeout (5 * 1000);
Conn.setrequestproperty ("Content-type", "Application/json");
Conn.setrequestproperty ("User-agent", "mozilla/5.0" (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/39.0.2171.71 safari/537.36 ");
Conn.setrequestproperty ("Host", "www.sjlive.cn");
Conn.setrequestproperty ("Key", key);
Establish a real HTTP connection
Conn.connect ();
Gets the output stream corresponding to the URLConnection object
out = new OutputStreamWriter (Conn.getoutputstream (), "UTF-8");
Send Request parameters
Out.write (param);
Buffer for flush output stream
Out.flush ();
Defines the response of the BufferedReader input stream to read the URL
in = new BufferedReader (New InputStreamReader (Conn.getinputstream (), "Utf-8"));
String Line;
while (line = In.readline ()) = null) {
result = result + line + "\ r \ n";
}
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Send POST request exception!") "+ e);
E.printstacktrace ();
} finally {
try{
if (out! = null) {
Out.close ();
}
if (in = null) {
In.close ();
}
}
catch (IOException ex) {
Ex.printstacktrace ();
}
}
return result;
}
}

Java HTTP basic framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.