Project users put forward new requirements, the original project within the platform to send information sent to the mobile phone, fortunately they already have a message sent platform, as long as the call its interface to send it.
The SMS delivery interface is a Web page that is implemented using JSP, which is called to send data to the Web page in a post manner.
Finding data on the web is almost always the same result:
System.Net.WebRequest req = System.Net.WebRequest.Create (URI);
Req. Proxy = new System.Net.WebProxy (proxystring, true);
And then according to user-given interface description and Java example modification, the result is always return the result is garbled, and then to the online search, said is the coding way of the problem, there is no way, only a number of attempts. After nearly a day of continuous testing, finally succeeded. My correct code is as follows:
Protected string sendmsg (String xmlmsg)
{
String urlpage = "http://www.handtimes.com/interface/forSCMIS.jsp";
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Note that this is the encoding method that I've been trying for a long time and the content of the XML content encoding
Encoding Encoding = encoding.getencoding ("GBK");
byte[] data = encoding. GetBytes (XMLMSG);
Prepare request ...
Setting parameters
Request = WebRequest.Create (urlpage) as HttpWebRequest;
Request. method = "POST";
The content type of this place is required on the interface document and must be the case
Request. ContentType = "Text/plain";
Request. ContentLength = data. Length;
OutStream = Request. GetRequestStream ();
OutStream. Write (data, 0, data. Length);
OutStream. Flush ();
OutStream. Close ();
Send request and get corresponding response data
Response = Request. GetResponse () as HttpWebResponse;
Until request. The GetResponse () program only starts sending post requests to the destination Web page
Instream = Response. GetResponseStream ();
sr = new StreamReader (instream, encoding);
Return results page (HTML) code
String content = Sr. ReadToEnd ();
return content;
}
To illustrate, the encoding of the data to be sent and the encoding of the content (XML) that was sent was successful using the GBK encoding,
Because the user to my account can not be sent to my own mobile phone, so I do not dare to do too many attempts, after the success did not continue to try, do not know the impact of the return of the content is garbled which code, or two are affected.
Req. ContentType = "application/x-www-form-urlencoded";
Req. method = "POST";
byte [] bytes = System.Text.Encoding.ASCII.GetBytes (Parameters);
Req. contentlength = bytes. Length;
System.IO.Stream OS = req. GetRequestStream ();
Os. Write (bytes, 0, bytes. Length);
Os. Close ();
System.Net.WebResponse resp = req. GetResponse ();
if (resp== null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader (resp. GetResponseStream ());
return Sr. ReadToEnd (). Trim ();