About the garbled characters (source code) during communication with C # through HTTP)

Source: Internet
Author: User

I am a beginner in the field of J2EE. I just started to learn Chinese garbled characters when I tried to communicate with the HTTP site by using the agent. I read some of my views on BBS. The simplest and most efficient way is to communicate with JSP: WRITEUTF and READUTF. However, since the target site I want to connect to is C #, I can use it with the smallest changes.
Solution! Set the encoding in Web. config in the C # project to UTF-8 (default: UTF-8 ).
The following is the source code of j2-based architecture:
Import javax. microedition. midlet .*;
Import javax. microedition. lcdui .*;
Import javax. microedition. io .*;
Import java. io .*;
Import java. lang. String ;;
Public class HttpTest extends MIDlet
{
Private Display display;
Public HttpTest ()
{
Display = Display. getDisplay (this );
}
Protected void startApp () throws MIDletStateChangeException
{
Try {
String url = "http: // 192.168.0.25/lt;/ webform1.aspx? Phone = 13800000000 & msg1 = c2 ";
HttpConnection hc = (HttpConnection) Connector. open (url );
DataInputStream dis = new DataInputStream (hc. openInputStream ());
Byte [] myData = new byte [100];
String content = "";

Dis. read (myData, 0, myData. length );
Content = new String (myData, "UTF-8 ");
Form f = new Form ("HTTP Test ");
F. append (content );
Display. setCurrent (f );
}
Catch (Exception e)
{
System. out. println (e. toString ());
Yydestroyed ();
}
}

Protected void pauseApp ()
{
}
Protected void destroyApp (boolean arg0) throws MIDletStateChangeException
{
}
}
The code structure is very simple, so it does not add instructions or instructions. I hope it will be helpful for beginners like me. I also hope that the old birds will love us more !!!!

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.