J2ME Network Programming (httpconnection) __ Programming

Source: Internet
Author: User
Tags response code

1, What is the difference between ' HTTP connection ' and ' socket connection '?
If you are choose to use "Socket connection", Opera Mini would keep an open connection to the Opera Mini servers. This means that pages'll load quicker, and the Opera Mini servers can push updates (for example new bookmarks through Op ERA Link) directly to Opera Mini on your phone.

If you are choose "HTTP Connection", Opera Mini has to establish a connection to the Opera Mini servers every time you load a Page. New updates to Opera Mini would also the not being pushed to the phone until a new page is loaded. Note:not all phone models and mobile networks allow socket connections. If you are have problems with your connection, try to use "HTTP connection" instead.

2, when the use of HTTP connection in j2me,you Needn ' t set a connection property, then by default it's set to keep-a Live (MIDP uses a subset of HTTP 1.1, which has persistent connections by default).

3, client, server interaction example code

Server (C #):

Public partial class CheckVersion:System.Web.UI.Page {private string version = "1.2.0";//Latest version private String URL = "HT Tp://****.jad "; Latest version download address private soft_version SV = new Soft_version (); protected void Page_Load (object sender, EventArgs e) {if (request.querystring["brand"]!= null) && Request.query string["model"]!= null) {if (request.querystring["ver"]!= null) {sv. Getnewversion (request.querystring["brand"). ToString (), request.querystring["model"]. ToString (), out version, out URL); if (version!= request.querystring["ver"). ToString ()) {Response.ContentType = "text/html"; Response.AddHeader ("content-length", url. ToCharArray (). Length.tostring ()); Response.Write (URL); else {String SS = ""; Response.ContentType = "text/html"; Response.AddHeader ("Content-length", SS. ToCharArray (). Length.tostring ()); Response.Write (ss); }} else {String SS = ""; Response.ContentType = "text/html"; Response.AddHeader ("Content-length", SS. ToCharArray (). Length.tostring ()); Response.wRite (ss); } } }

Client (J2ME):

/** * http send request, return response * @author Jessica Tang */public class SendRequest {/** * send request, return response * @param strrequest to serve The data sent by the service * @param serverurl server URL * @return responsecode+ content Value/public static string Send (String strrequest,string serveru RL) {String message= ""; try {httpconnection HC = (httpconnection) connector.open (ServerURL, connector.read_write);//settings please The method is POST Hc.setrequestmethod (httpconnection.post); Send data to server outputstream OS = Hc.openoutputstream (); byte[] Request_body = Strrequest.getbytes (); for (int i = 0; i < request_body.length i++) {os.write (request_body[i);} os.flush (); Os.close (); OS = null; Read response code from server: 200-Normal message = String.valueof (Hc.getresponsecode ()); Reading data from the server InputStream is = Hc.openinputstream (); Read data int ch; while (ch = is.read ())!=-1) {message = message + (char) ch;;} is.close (); is = null; Hc.close (); Close Httpconnection Connection HC = NULL; catch (Exception e) {e.printstacktrace ();} return message; } }

/** * Check if there is a new version * @author Jessica Tang */private void CheckNew () {String str = sendrequest.send ("CheckNew", Locale.get ("U Rl.checknew ")); if (Str.trim (). Equals ("200")) {//is the latest Version//#style notificationalert Alert alert = new Alert ("Hint", "the version is already up to date.") ", null, alerttype.info); Display.setcurrent (alert); }else{try {this.platformrequest (str);} catch (Connectionnotfoundexception e) {//#style notificationalert Alert alert = New Alert ("Prompt", "version update failed.") ", null, alerttype.error); Display.setcurrent (alert); } } }

Appendix: Common Response Headers and their meaning

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.