- GET:
//設定GET資料 Uri t_uri = new Uri("http://localhost/Test/Get.asp?id=yilin"); 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:
string t_url = "http://localhost/Test/Post.asp"; 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]
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"; URL t_url = new URL("http://localhost/Test/hello.asp"); 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(); } }
沒有留言:
張貼留言