Xamarin android uses the http protocol to upload strings to the server. xamarinandroid

Source: Internet
Author: User

Xamarin android uses the http protocol to upload strings to the server. xamarinandroid

I. first understand the http protocol
Ii. Upload using http protocol
Public static string Upload (string urlPath, string content)
{
String mContent = "";
Try
{
String BOUNDARY = "--- 7d4a6d158c9"; // defines the data separation line.
URL url = new URL (urlPath );
HttpURLConnection conn = (HttpURLConnection) url. OpenConnection ();
// You must set the following two rows to send a POST request:
Conn. DoOutput = true;
Conn. DoInput = true;
Conn. UseCaches = false;
Conn. RequestMethod = "POST ";
Conn. SetRequestProperty ("connection", "Keep-Alive ");
Conn. SetRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )");
Conn. SetRequestProperty ("Charsert", "UTF-8 ");
Conn. SetRequestProperty ("Content-Type", "application/x-www-form-urlencoded; boundary =" + BOUNDARY );
Java. IO. OutputStream OS = new Java. IO. DataOutputStream (conn. OutputStream );
Java. Lang. StringBuilder sb = new Java. Lang. StringBuilder ();
/Set header Parameters/
Sb. Append ("-");
Sb. Append (BOUNDARY );
Sb. Append ("\ r \ n ");

/* Fill in content */sb. append ("Content-Disposition: form-data; name = \" userName \ "\ r \ n"); sb. append (content); sb. append ("\ r \ n");/* sets the end parameter */sb. append ("\ r \ n --"); sb. append (BOUNDARY); sb. append ("-- \ r \ n"); byte [] data = Encoding. UTF8.GetBytes (sb. toString (); OS. write (data); OS. flush (); OS. close (); // defines the BufferedReader input stream to read the URL response Java. IO. bufferedReader reader = new Java. IO. bufferedReader (new Java. IO. inputStream Reader (conn. InputStream); string line = null; while (line = reader. ReadLine ())! = Null) {mContent + = line;} System. console. writeLine (mContent + "//"); return mContent ;} catch (Java. lang. exception e) {// System. out. println ("an exception occurred when sending the POST request! "+ E); // e. printStackTrace ();} return mContent ;}

The code for receiving the value on the server side is:
<% @ Page import = "com. jspsmart. upload. SmartUpload" %>
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<%
SmartUpload su = new SmartUpload ();
// Initialize the component Context
Su. initialize (pageContext );
// Set the Encoding
Su. setCharset ("UTF-8 ");
Try {
Su. upload ();
} Catch (Exception e ){

} String clientContent = su. getRequest (). getParameter ("userName"); System. out. print ("the user name passed from the android client is:" + clientContent );

%>

In this way, after clicking the Button of the client, the admin string is saved and printed in the java server console.

3. You can use http to upload data to the server.
Private string UpLoadUserName (string serverUrl, string content)
{
String mContent = "";
Try
{
URL url = new URL (serverUrl );
HttpURLConnection conn = (HttpURLConnection) url. OpenConnection ();
// You must set the following two rows to send a POST request:
Conn. DoOutput = true;
Conn. DoInput = true;
Conn. UseCaches = false;
Conn. RequestMethod = "POST ";
Conn. SetRequestProperty ("connection", "Keep-Alive ");
Conn. SetRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )");
Conn. SetRequestProperty ("Charsert", "UTF-8 ");
// Conn. SetRequestProperty ("Content-Type", "application/x-www-form-urlencoded; boundary =" + BOUNDARY );
Java. IO. OutputStream OS = new Java. IO. DataOutputStream (conn. OutputStream );
Java. Lang. StringBuilder sb = new Java. Lang. StringBuilder ();
Sb. Append ("userName =" + content );
Byte [] data = Encoding. UTF8.GetBytes (sb. ToString ());
OS. Write (data );
OS. Flush ();
OS. Close ();
// Define the BufferedReader input stream to read the URL response
Java. IO. BufferedReader reader = new Java. IO. BufferedReader (new Java. IO. InputStreamReader (conn. InputStream ));
String line = null;
While (line = reader. ReadLine ())! = Null)
{
MContent + = line;
}
System. Console. WriteLine (mContent + "//");
Return mContent;
}
Catch (Java. Lang. Exception e)
{
System. Console. WriteLine ("An error occurred while sending the POST request !!!" + E );
}
Return mContent;

 }

Code for receiving data from the server:
<% @ Page import = "com. jspsmart. upload. SmartUpload" %>
<% @ Page import = "net. sf. json. JSONObject" %>
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>

<%
String getName = request. getParameter ("userName ");
String clientContent = new String (getName. getBytes ("iso-8859-1"); // prevents garbled characters
System. out. print (clientContent );
%>
The difference is that the server uses the system's own request class to receive client requests without using the http protocol!
If you use http requests, the server uses a third-party library, SmartUpLoad, to receive data from the client!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.