Solution: Java. Io. ioexception: server returned HTTP response code: 500

Source: Internet
Author: User
Tags response code

It is easy to use Java to implement cross-origin access. It turns out that the Java net package can be used to implement connection URL, post, get value or something.

Of course, you can also use a third-party method like httpclient.

 

The problem encountered today is that an exception occurs during post:

Java. Io. ioexception: server returned HTTP response code: 500 for URL: http: // 192.98.8.50 /...

I checked it on the Internet, which is also a lot of situations,

I encountered the following problems:

I opened outputstreamwriter without the post parameter.

The modified complete program is as follows:

Private stringbuffer urlconnectionpost (string tourl, stringbuffer data) {<br/> stringbuffer sb = NULL; <br/> bufferedreader reader = NULL; <br/> outputstreamwriter wR = NULL; <br/> URL; <br/> try {<br/> url = new URL (tourl); <br/> urlconnection conn = URL. openconnection (); <br/> Conn. setdooutput (true); <br/> Conn. setconnecttimeout (1000*5); <br/> // enable outputstreamwriter only when there is a post value <br/> If (dat A! = NULL & data. tostring (). trim (). length ()> 0) {<br/> wR = new outputstreamwriter (Conn. getoutputstream (), "UTF-8"); <br/> Wr. write (data. tostring (); <br/> Wr. flush (); <br/>}</P> <p> // get the response <br/> reader = new bufferedreader (New inputstreamreader (conn <br/>. getinputstream (), "UTF-8"); <br/> Sb = new stringbuffer (); <br/> string line = NULL; <br/> while (line = reader. readline ())! = NULL) {<br/> Sb. append (LINE + "/N"); <br/>}< br/>} catch (ioexception E) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}finally {<br/> try {<br/> If (WR! = NULL) {<br/> Wr. Close (); <br/>}< br/> If (reader! = NULL) {<br/> reader. close (); <br/>}< br/>} catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> return sb; <br/>}

Related Article

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.