C#POST 数据

 

 1      string url = "http://192.168.1.127/xia/Server/xia.asp?COMMAND=Login&Account=puretime&Password=12345";
 2
 3            CookieContainer cookie = new CookieContainer ();
 4
 5            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
 6
 7            myRequest.AllowAutoRedirect = true;
 8
 9            myRequest.CookieContainer = cookie;
10
11            HttpWebResponse myresponse = (HttpWebResponse)myRequest.GetResponse();
12
13            myresponse.Cookies = cookie.GetCookies(myRequest.RequestUri);
14
15            Stream mystream = null;
16
17            mystream = myresponse.GetResponseStream();
18
19            StreamReader myreader = new StreamReader(mystream, System.Text.Encoding.Default, true);
20
21            string pagefile = myreader.ReadToEnd();
22
23            if (pagefile.IndexOf("<Value>1</Value>"> 0)//登入成功
24              {
25              
26                string postUrl = "http://192.168.1.127/xia/Server/xia.php?COMMAND=AddContent";           
27
28                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(postUrl);
29
30                Request.CookieContainer = cookie;
31
32                Request.Method = "POST";
33
34                Request.ContentType = "application/x-www-form-urlencoded";
35
36                Request.AllowAutoRedirect = true;
37
38                string strXML =<book><author>Irina</author><title>Piano Fort A</title><price>4.95</price></book>";
39
40           //     strXML = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + strXML;
41
42                byte[] data = Encoding.UTF8.GetBytes(strXML);
43
44                Stream newStream = Request.GetRequestStream();
45          
46                newStream.Write(data, 0, data.Length);
47             
48                newStream.Close();
49
50               HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
51 
52               Stream stream = null;
53
54               stream = response.GetResponseStream();
55
56               StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true);
57
58               string file = reader.ReadToEnd();
59
60               myreader.DiscardBufferedData();
61
62               myreader.Close();
63
64               stream.Close();
65
66               FileStream s = new FileStream("c:/test.txt", FileMode.Create);
67
68               StreamWriter w = new StreamWriter(s, System.Text.Encoding.Default);
69
70               w.Write(file);
71
72               w.Close();

 

posted @ 2009-09-01 13:45  聖光小草  阅读(323)  评论(0编辑  收藏  举报