Description: Returns a string in which Chinese garbled problems may appear
<span style= "WHITE-SPACE:PRE;" > </span>///<summary>///C # POST send XML///</summary>///<par Am name= "url" > Target url</param>///<param name= "Strpost" > String to post (data) </param>///&
Lt;returns> Server Response </returns> private string postxml (string url, string strpost) { string result = String.
Empty; Our postvars//asciiencoding.ascii.getbytes (string str)//is to put string str in Simplified Chinese (asciiencoding.asci
I) The encoding mode,//encoding into bytes type of byte stream array;
Note that this is the encoding method, as well as the content of the XML content encoding, if not pay attention to the corresponding will appear at the end of the error byte[] buffer = Encoding.UTF8.GetBytes (strpost);
StreamWriter mywriter = null; Create a HttpWebRequest object based on the URL//initialisation, we use localhost, change if appliable httpwebreque
St Objrequest = (HttpWebRequest) webrequest.create (URL); Our methodis post, otherwise the buffer (postvars) would be useless Objrequest.method = "POST";
The length of the buffer (Postvars) is used as contentlength.
Set the content length of the string being posted. objrequest.contentlength = buffer.
Length;
We use Form ContentType, for the postvars.
Set the content type of the data being posted. Objrequest.contenttype = "Text/xml";//Submitting XML//objrequest.contenttype = "application/x-www-form-urlencoded"; Submit a form try {//we Open a stream for writing the Postvars MyW
Riter = new StreamWriter (Objrequest.getrequeststream ()); Now we write, and afterwards, we close.
Closing is always important!
Mywriter.write (Strpost);
catch (Exception e) {return e.message;
} finally {mywriter.close ();
//Read server return information//get The response handle, we have no true response yet! This paper url:http://www.bianceng.cn/programming/csharp/201410/45576.htm httpwebresponse objResponse = (HttpWebRespo
NSE) Objrequest.getresponse (); Using as a statement that defines a range at the end of which the object using is disposed (StreamReader sr = new StreamReader (Objresponse.getresponsestream ()) {//readtoend applies to small file reads, a one-time return to the entire file result = Sr.
ReadToEnd (); Sr.
Close ();
return result; }
Background:
I sent the XML string in encoding= ' UTF-8 ' format, but at first I used the
Encoding.Unicode.GetBytes (strpost) or default or ASCII prompts for an error.
After modifying the character encoding format, success! So choose the right method based on the format you send.
Attention:
The function can send XML to the Ufida side, but the return information in the character is garbled.
This function can also achieve the same function but avoids the garbled problem
The detailed procedure and code are as follows:
1, create HttpWebRequest objects, HttpWebRequest can not be created directly through new, only through the WebRequest.Create (URL) way to obtain. WebRequest is a unified portal (Factory mode) that obtains some application-layer protocol objects, which determine the type of object that is ultimately created, based on the protocol of the parameters.
2, initialize the HttpWebRequest object, this process provides some HTTP request commonly used header attributes: Agentstring,contenttype, which agentstring more interesting, it is used to identify your browser name, By setting this property you can spoof the server you are a ie,firefox or even a Mac inside Safari. Many carefully designed sites will return code that is optimized for different browsers based on this value.
3, attach to the data to post to the server to the HttpWebRequest object, the process of attaching post data is special, it does not provide a property to user access, need to write HttpWebRequest object provided in a stream inside.
4, read the server's return information, read the server back, pay attention to return the data encoding, if we provide the type of decoding is not correct, will cause garbled, more common is utf-8 and gb2312. Typically, the site will encode it in the HTTP header, and if not, we can only determine how it is encoded by the statistical method that returns the binary value.