ÏòÖ¸¶¨URL·¢ËÍGET·½·¨µÄÇëÇó£¨java£©
ʹÓÃJava·¢ËÍGET¡¢POSTÇëÇó
——½ÚÑ¡×Ô¡¶·è¿ñJava½²Òå¡·
URLµÄopenConnection()·½·¨½«·µ»ØÒ»¸öURLConnection¶ÔÏ󣬸öÔÏó±íʾӦÓóÌÐòºÍ URL Ö®¼äµÄͨÐÅÁ´½Ó¡£³ÌÐò¿ÉÒÔͨ¹ýURLConnectionʵÀýÏò¸ÃURL·¢ËÍÇëÇ󡢶ÁÈ¡URLÒýÓõÄ×ÊÔ´¡£
ͨ³£´´½¨Ò»¸öºÍ URL µÄÁ¬½Ó£¬²¢·¢ËÍÇëÇ󡢶ÁÈ¡´Ë URL ÒýÓõÄ×ÊÔ´ÐèÒªÈçϼ¸¸ö²½Ö裺
£¨1£©Í¨¹ýµ÷ÓÃURL¶ÔÏóopenConnection()·½·¨À´´´½¨URLConnection¶ÔÏó¡£
£¨2£©ÉèÖÃURLConnectionµÄ²ÎÊýºÍÆÕͨÇëÇóÊôÐÔ¡£
£¨3£©Èç¹ûÖ»ÊÇ·¢ËÍGET·½Ê½ÇëÇó£¬Ê¹ÓÃconnect·½·¨½¨Á¢ºÍÔ¶³Ì×ÊÔ´Ö®¼äµÄʵ¼ÊÁ¬½Ó¼´¿É£»Èç¹ûÐèÒª·¢ËÍPOST·½Ê½µÄÇëÇó£¬ÐèÒª»ñÈ¡URLConnectionʵÀý¶ÔÓ¦µÄÊä³öÁ÷À´·¢ËÍÇëÇó²ÎÊý¡£
£¨4£©Ô¶³Ì×ÊÔ´±äΪ¿ÉÓ㬳ÌÐò¿ÉÒÔ·ÃÎÊÔ¶³Ì×ÊÔ´µÄÍ·×ֶΡ¢»òͨ¹ýÊäÈëÁ÷¶ÁÈ¡Ô¶³Ì×ÊÔ´µÄÊý¾Ý¡£
ÔÚ½¨Á¢ºÍÔ¶³Ì×ÊÔ´µÄʵ¼ÊÁ¬½Ó֮ǰ£¬³ÌÐò¿ÉÒÔͨ¹ýÈçÏ·½·¨À´ÉèÖÃÇëÇóÍ·×ֶΣº
qsetAllowUserInteraction£ºÉèÖøÃURLConnectionµÄallowUserInteractionÇëÇóÍ·×ֶεÄÖµ¡£
qsetDoInput£ºÉèÖøÃURLConnectionµÄdoInputÇëÇóÍ·×ֶεÄÖµ¡£
qsetDoOutput£ºÉèÖøÃURLConnectionµÄdoOutputÇëÇóÍ·×ֶεÄÖµ¡£
qsetIfModifiedSince£ºÉèÖøÃURLConnectionµÄifModifiedSinceÇëÇóÍ·×ֶεÄÖµ¡£
qsetUseCaches£ºÉèÖøÃURLConnectionµÄuseCachesÇëÇóÍ·×ֶεÄÖµ¡£
³ý´ËÖ®Í⣬»¹¿ÉÒÔʹÓÃÈçÏ·½·¨À´ÉèÖᢻòÔö¼ÓͨÓÃÍ·×ֶΣº
qsetRequestProperty(String key, String value)£ºÉèÖøÃURLConnectionµÄkeyÇëÇóÍ·×ֶεÄֵΪvalue¡£ÈçÏ´úÂëËùʾ£º
conn.setRequestProperty("accept" , "*/*")
qaddRequestProperty(String key, String value)£ºÎª¸ÃURLConnectionµÄkeyÇëÇóÍ·×ֶεÄÔö¼ÓvalueÖµ£¬¸Ã·½·¨²¢²»»á¸²¸ÇÔÇëÇóÍ·×ֶεÄÖµ£¬¶øÊǽ«ÐÂÖµ×·¼Óµ½ÔÇëÇóÍ·×Ö¶ÎÖС£
µ±Ô¶³Ì×ÊÔ´¿ÉÓÃÖ®ºó£¬³ÌÐò¿ÉÒÔʹÓÃÒÔÏ·½·¨ÓÃÓÚ·ÃÎÊÍ·×ֶκÍÄÚÈÝ£º
qObject getContent()£º»ñÈ¡¸ÃURLConnectionµÄÄÚÈÝ¡£
qString getHeaderField(String name)£º»ñÈ¡Ö¸¶¨ÏìӦͷ×ֶεÄÖµ¡£
qgetInputStream()£º·µ»Ø¸ÃURLConnection¶ÔÓ¦µÄÊäÈëÁ÷£¬ÓÃÓÚ»ñÈ¡URLConnectionÏìÓ¦µÄÄÚÈÝ¡£
qgetOutputStream()£º·µ»Ø¸ÃURLConnection¶ÔÓ¦µÄÊä³öÁ÷£¬ÓÃÓÚÏòURLConnection·¢ËÍÇëÇó²ÎÊý¡£
×¢Ò⣺Èç¹û¼ÈҪʹÓÃÊäÈëÁ÷¶ÁÈ¡URLConnectionÏìÓ¦µÄÄÚÈÝ£¬Ò²ÒªÊ¹ÓÃÊä³öÁ÷·¢ËÍÇëÇó²ÎÊý£¬Ò»¶¨ÒªÏÈʹÓÃÊä³öÁ÷£¬ÔÙʹÓÃÊäÈëÁ÷¡£
getHeaderField·½·¨ÓÃÓÚ¸ù¾ÝÏìӦͷ×Ö¶ÎÀ´·µ»Ø¶ÔÓ¦µÄÖµ¡£¶øijЩͷ×Ö¶ÎÓÉÓÚ¾³£ÐèÒ
Ïà¹ØÎĵµ£º
Õâ±¾ÊéÏêϸ½²½âÁËģʽÀíÂÛµÄÓÉÀ´£¨ºÜ¶àÈË¿ÉÄܲ»ÖªµÀģʽÀíÂÛÓëÖйúµÀ¼Ò˼ÏëµÄÔ¨Ô´£©£¬ÃæÏò¶ÔÏóµÄÈí¼þÉè¼ÆÔÔò£¬Ò»Ö±½²µ½¶þÊ®¼¸¸öÉè¼Æģʽ£¬ÒÔ¼°ÕâЩģʽÔÚJavaÓïÑÔÖеÄʵÏֵȵȡ£ÊéÖиø³öÁË´óÁ¿À´×Ô»ª¶û½ÖϵͳµÄÕæʵÏîÄ¿µÄÀý×Ó£¬ÓÃÀ´½²½âÉè¼ÆģʽÔÚϵͳÉè¼ÆÖеÄÓ¦ÓᣠÕâ±¾Êé´¦ÓÚÀíÂÛºÍʵ¼ùÖ®¼ä£¬ÊÇÒ»±¾¶¨Î»¶ÀÌØ¡¢·ç¸ñ¶ÀÌص ......
package com.tienway.util;
import java.util.ArrayList;
public class StringUtil
{
public static String ContextType1ToType2(String Type1Context)
{
if (Type1Context== null || Type1Context.length() == 0)
{
return "";
}
char[] c = Type1Con ......
String s = new Date().toString();¿ÉÒÔÖ±½Ó´´½¨ÍêжÔÏóºó¾ÍÁ¢¼´ÓÃÆäµÄ·½·¨¡£
Ò»¸ö¶ÔÏó±äÁ¿²¢Ã»ÓÐʵ¼Ê°üº¬Ò»¸ö¶ÔÏ󣬶øÊǽö½öÒýÓÃÒ»¸ö¶ÔÏó¡£
GregorianCalendarÀà
javaÉè¼ÆÕß¾ö¶¨°Ñ±£´æʱ¼äÓë¸øʱ¼äµãÃüÃû·Ö¿ªÀ´£¬ÕâÑù¾ÍÓÐÁ½¸öÀà,dateºÍGregorianCalendar.Ò»¸öÓÃÀ´±íʾʱ¼äµã£¬Ò»¸öÓÃÀ´±íʾ´ó¼ÒÊìϤµÄÈÕÀú±íʾ·¨¡£ÊÂ浃 ......
×÷Õß
Josh Long
ÒëÕß
ËÎçâ
·¢²¼ÓÚ
2010Äê5ÔÂ5ÈÕ ÏÂÎç9ʱ7·Ö
Josh BlochÔÚSun¹«Ë¾¶àÄêΪJavaƽ̨×÷³öÁ˽ܳö¹±Ï×£¨Èç¹ûÄãÓùýJava
Collections¿ò¼Ü¾Í»áÁ˽âÕâÒ»µã£©£¬ÏÖÔÚGoogle¾ÍÖ°£¬ÊÇ»ñ½±Í¼Ê顶Effective ......