2009年1月7日 星期三

How to Set porxy, port in java

由於一般公司的 proxy 會擋住一些對外的網路訊息,但是一般80 port 都可以使用 ,所以利用 80 port 傳送和取得資料,以利能利用google 的網路服務

大致要做兩個設定

1. 查詢proxy server Name

1-1 Enter DOS
(開始->執行->cmd)
1-2 Enter Netstate 以查詢server Name
(公司的proxy server Name: EX proxy.XXXX.corp)





2. Setting
2-1 import java.util.Properties
2-2 Add code
Properties systemSettings = System.getProperties();

systemSettings.put("http.proxyHost",
"myProxyServer.com");
myProxyServer.com
要取代為剛查詢 的 proxy server name

systemSettings.put("http.proxyPort", "80");
Set port 80 的原因是在一般狀況 80是 可以通的

Reference

Dos網路指令
http://www.pczone.com.tw/vbb3/archive/t-17870.html
Java proxy 設定
http://www.rgagnon.com/javadetails/java-0085.html
Java with google api
http://code.google.com/intl/zh-TW/apis/gdata/articles/eclipse.html

以下是兩個java 的範例

Example 1
輸入網址取得html的編碼

Example 2
取得google document 的文件列表

Example 1
-------------------------------------------------------------------------------------
import java.net.*;
import java.io.*;
import java.util.Properties;

public class URLUtils {
public static void main(String s[]) {
URLUtils.dump("http://www.yahoo.com");
System.out.println("**************");
URLUtils.dump("http://www.google.com");
System.out.println("**************");
}

public static void dump(String URLName){
try {
DataInputStream di = null;
FileOutputStream fo = null;
byte [] b = new byte[1];

// PROXY
Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost","proxy.XXXX.corp") ;
systemSettings.put("http.proxyPort", "80") ;

URL u = new URL(URLName);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
//
// it's not the greatest idea to use a sun.misc.* class
// Sun strongly advises not to use them since they can
// change or go away in a future release so beware.
//
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
String encodedUserPwd =
encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes());
con.setRequestProperty
("Proxy-Authorization", "Basic " + encodedUserPwd);
// PROXY ----------

di = new DataInputStream(con.getInputStream());
while(-1 != di.read(b,0,1)) {
System.out.print(new String(b));
}
}
catch (Exception e) {
e.printStackTrace();
}
}

}

-------------------------------------------------------------------------------------

Example 2 修改帳號密碼才能RUN
-------------------------------------------------------------------------------------

import java.net.URL;
import java.util.Properties;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;

public class TsetDocumentList {

public static void main(String[] args) {

Properties systemSettings = System.getProperties();
systemSettings.put("proxyPort","80");
systemSettings.put("proxyHost","proxy.XXXXX.corp");
//System.out.println("using proxy: "+ System.getProperties() );

try{
DocsService service = new DocsService("Document List Demo");
service.setUserCredentials("XXXXXXX...@gmail.com", "yourCode");
//service.setUserCredentials( yourAccount, yourCode);

URL documentListFeedUrl= new
URL("http://docs.google.com/feeds/documents/private/full");

DocumentListFeed feed = service.getFeed(documentListFeedUrl,
DocumentListFeed.class);

for (DocumentListEntry entry : feed.getEntries()){
System.out.println(entry.getTitle().getPlainText());
}
}

catch(Exception ex){
System.err.println("Expection:"+ex.getMessage());

沒有留言: