C # .net analog form is submitted POST

方法一、
System.Net.WebClient WebClientObj        = new System.Net.WebClient();
   System.Collections.Specialized.NameValueCollection PostVars  = new System.Collections.Specialized.NameValueCollection();
   PostVars.Add("A1","0");
   PostVars.Add("A2","0");
   PostVars.Add("A3","000");

   the try
   {
    byte [] = byRemoteInfo WebClientObj.UploadValues ( "http://www.lovezhao.com/vote.asp", "the POST", PostVars);
    useless // below it, above it can be a word
    string = System.Text.Encoding.Default.GetString sRemoteInfo (byRemoteInfo);  
    // this is to obtain return information
    richTextBox_instr.Text + = sRemoteInfo;
   }
   the catch
   {}



方法二、
string url = "网址";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string s = "要提交的数据";
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes (LoginInfo);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes,0,requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr.ReadToEnd(); Response.Write(line); sr.Close(); res.Close(); 

 

1. Creating httpWebRequest objects HttpWebRequest can not be directly created by new, can only be obtained by WebRequest.Create (url) way. Get some WebRequest is a single inlet (plant model) application layer protocol object column, which is determined according to the type of object to create the final protocol parameters. So our program which has a process for testing the return type of the object. 2. HttpWebRequest object to initialize this process http requests to provide some common properties: agentstring, contenttype, etc. which agentstring more interesting, it is used to identify the name of the browser you use, and by setting this property you can cheat a server you are IE, firefox even mac is inside the safari. Many carefully designed sites will be returned to the user's browser is particularly optimized code based on this value.  
 
 
  

3. Additional to the data server to POST HttpWebRequest object to additional POST data special process, it does not provide a user access to the property, you need to write a Stream HttpWebRequest object provided inside. 4. Read the server sends back to read when the server returns, pay attention to return data encoding. If we provide the type of decoding does not cause garbled. More common is the confusion between gb2312 utf-8 and, according to my tests, the host country are generally gb2312 encoding. Good general design of the site will encode its way on the return of the http header inside, but there are a lot of sites do not have, we can only determine its encoding of binary values returned by a statistical method.  
 
  

        /// <Summary> 
        /// 
        /// </ Summary> 
        /// <param name = "URL"> address </ param> 
        /// <param name = "Method"> method </ param> 
        // / <name = "param" param > json parameters </ param> 
        /// <Returns> </ Returns> 
        public static String WebServiceApp (String URL, Method String, String param) 
        { 
            // coding type converting an input parameter, the bytep [] array 
            byte [] = byteArray the Encoding.UTF8.GetBytes ( "JSON =" + param); 
            // initialize a new webRequst 
            //. 1. Create Object httpWebRequest 
            HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create ( new Uri (url + "/" + method)); 
            //2. HttpWebRequest object initialization
            = webRequest.ContentType "file application / X-WWW-form-urlencoded"; 
            webRequest.ContentLength = byteArray.Length; 
            //. 3. Additional to the data server to POST HttpWebRequest object (POST process additional data is rather special, it does not provide a user access to the property, needs to write a Stream HttpWebRequest object provided inside.) 
            Stream NewStream webRequest.GetRequestStream = () ; // create a Stream, a stream assignment is written inside HttpWebRequest object provides 
            newStream.Write (byteArray, 0, byteArray.Length); 
            newStream.Close (); 
            // 4. Read the server sends back 
            the HttpWebResponse Response = (the HttpWebResponse) WebRequest.GetResponse ();  
            the StreamReader = new new PHP the StreamReader (response.GetResponseStream (), Encoding.UTF8);
            String phpend php.ReadToEnd = (); 
            return phpend; 
           
        }

Guess you like

Origin www.cnblogs.com/efreer/p/11362578.html