public String getContentByHttpURLConnection(String gateway_url,String encoding, String methodStr)

{
String respond = "";
HttpURLConnection conn = null;
try
{
byte[] postData = "".getBytes("UTF-8");

 

URL url = new URL(gateway_url);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod(methodStr);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset="+encoding);
conn.setRequestProperty("Content-Length",
Integer.toString(postData.length));

 

OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();

 

respond = conn.getResponseCode() + " "+ conn.getResponseMessage() ;
System.out.println(respond);
InputStream ist = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(ist,encoding));
StringBuffer sb = new StringBuffer();
char[] c = new char[1];
while(in.read(c,0,1)==1)
{
sb.append(c[0]);
}

 

respond = sb.toString();

 

sb.setLength(1);
in.close();
ist.close();

 

postData = null;
sb =null;
in =null;
ist = null;
out =null;
url =null;
} catch (Exception e) {
//e.printStackTrace();
respond = e.getMessage();
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
conn =null;
}

 

return respond;
}




重點在編碼格式參數

 

BufferedReader in = new BufferedReader(new InputStreamReader(ist,encoding));
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()