- GET: 12345678
//設定GET資料
HttpWebRequest t_req = System.Net.WebRequest.Create(t_uri)
as
HttpWebRequest;
t_req.Method =
"GET"
;
//取得回應的內容
System.Net.WebResponse t_resp = t_req.GetResponse();
System.IO.StreamReader t_stream =
new
System.IO.StreamReader(t_resp.GetResponseStream());
Console.WriteLine(t_stream.ReadToEnd().Trim());
- POST: 1234567891011
string
t_post =
"id=yilin&password=helloworld"
;
//將POST資料轉成byte array
byte
[] t_bytdata = Encoding.UTF8.GetBytes(t_post);
WebRequest t_req = WebRequest.Create(t_url);
//設定POST資料
t_req.ContentType =
"application/x-www-form-urlencoded"
;
t_req.Method =
"POST"
;
Stream t_ws = t_req.GetRequestStream();
t_ws.Write(t_bytdata, 0, t_bytdata.Length);
t_ws.Close();
[Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class JavaPost { public JavaPost() { try { String t_data = "T=HelloWorld" ; HttpURLConnection t_conn = (HttpURLConnection) t_url.openConnection(); t_conn.setRequestMethod( "POST" ); t_conn.setDoOutput( true ); OutputStream t_out = t_conn.getOutputStream(); t_out.write(t_data.getBytes()); t_out.close(); BufferedReader t_in = new BufferedReader( new InputStreamReader(t_conn.getInputStream())); while (t_in.ready()) { System.out.println(t_in.readLine()); } } catch (Exception e) { System.out.println(e); } } public static void main(String[] argv) { JavaPost t_post = new JavaPost(); } } |
沒有留言:
張貼留言