Sends a GET method to the specified URL to obtain a resource, encoding Problem.

Source: Internet
Author: User
Tags finally block

HTTP Encoding. today, encountering the data obtained on the Web page, using HTTP GET request to access the URL to obtain resources, the network has a corresponding method. I've never known anything before. rest style, now I want to just open a controller, and then make the person can call your back Code. ((value= "xxx"))

@Controller
public class Getwebdatacontroller {

Public list<jsonobject> roadlist = new Arraylist<jsonobject> ();
Public list<jsonobject> jamlist = new Arraylist<jsonobject> ();
Public list<jsonobject> numlist = new Arraylist<jsonobject> ();

/*public Getwebdatacontroller () {
System.out.println ("initialize getwebdatacontroller");
}*/

@Autowired
Private Insertalldataservice service;

@ResponseBody
@RequestMapping (value= "/getwebdata.htm", produces = "text/html;charset=utf-8")
Public String getwebdata (httpservletrequest request, httpservletresponse Response)
Throws parserconfigurationexception, saxexception, ioexception, documentexception{
This.setaccessheader (response);
Roadlist.clear ();
Jamlist.clear ();
Numlist.clear ();
/*string url1 = Com.enjoyor.soa.traffic.server.ubms.util.SpringReader.getProperty ("url1") */
String URL1 = "http://www.hzjtydzs.com/web/xmlsvc/currentroadspeed.aspx?rank=%e7%bb%86%e7%b2%92%e5%ba%a6& Order=asc&areaid=%e6%a0%b8%e5%bf%83%e5%8c%ba ";
String url2 = "http://www.hzjtydzs.com/web/xmlsvc/currentNumberData.aspx";
String Url3 = "http://www.hzjtydzs.com/web/xmlsvc/currentJamIndex.aspx";
String returnstring = "";
string[] urls = new string[] {url1, url2, url3};
GetNode getnode = new GetNode ();
for (int i = 0; i < urls.length; i++) {
String URL = urls[i];
String param = "";
String Filelac = Httprequest.sendget (url, param);

// error location, start because it is under the Tomcat server, there is no way to debug, only through the log and print to collect information, found to be documenthelper.parsetext (filelac); So I began to modify here, but soon found that the helper files are published by others, can not be modified, then can only be processed before This. Here is I did not think of, the thinking of the obstruction, I did not go into the HttpRequest to see, I thought this is also released a good class, did not expect to modify This. yes, mistakes occur in parsetext, but the real root of the problem is in the previous Code.
Document doc = Documenthelper.parsetext (filelac);
Element Rootelt = doc.getrootelement (); Get root node
Getnode.getnodes (rootelt, "");
If (url.indexof ("numberdata") > 0) {
Numlist = (list<jsonobject>) getnode.getnodes (rootelt, ""). get ("numlist");//traverse all nodes starting from the root node
}
If (url.indexof ("roadspeed") > 0) {
Roadlist = (list<jsonobject>) getnode.getnodes (rootelt, ""). get ("roadlist");
}
If (url.indexof ("jamindex") > 0) {
Jamlist = (list<jsonobject>) getnode.getnodes (rootelt, ""). get ("jamlist");
}
}
If (numlist.size () >0) {
Returnstring + = numlist.tostring ();
}
If (roadlist.size () >0) {
Returnstring + = roadlist.tostring ();
}
If (jamlist.size () >0) {
Returnstring + = jamlist.tostring ();
}
Return returnstring;
}

/**
* Cross-domain Declaration
* @param response
*/
private void Setaccessheader (httpservletresponse Response) {
Response.setheader ("access-control-allow-origin", "*");
}

}

Backup

Package com.enjoyor.soa.traffic.server.sjjx.http;

Import java.io.BufferedReader;
Import java.io.IOException;
Import java.io.InputStreamReader;
Import java.io.PrintWriter;
Import java.net.URL;
Import java.net.URLConnection;
Import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
Import org.apache.commons.httpclient.HttpClient;
Import org.apache.commons.httpclient.methods.GetMethod;
Import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpRequest {

/**
* Request to send a GET method to the specified URL
* URL to send request @param URL
* @param the PARAM request parameter, the request parameter should be in the form of name1=value1&name2=value2.
* @return The response result of the remote resource represented by the URL
*/
public static string Sendget (string url, string Param) {
String result = "";
String strURL = "";
try {

HttpClient httpclient=new HttpClient ();
GetMethod getmethod=null;
if (param = = "") {
strURL = url;
}else{
strURL = URL + "?" + param;
}
Getmethod=new GetMethod (strurl);
Getmethod.getparams (). setparameter (httpmethodparams.retry_handler, new Defaulthttpmethodretryhandler ());
Httpclient.executemethod (getmethod);
byte[] bresposebody = Getmethod.getresponsebody ();
String strresposebody = new String (bresposebody);
String strresposebody = new string (getmethod.getresponsebody (). getBytes ("iso8859-1"), "utf-8"); //modify the Code
result = strresposebody;

} catch (Exception E) {
System.out.println ("send GET Request Exception!") "+ e);
E.printstacktrace ();
}
Return result;
}

/**
* Request to send the Post method to the specified URL
* URL to send request @param URL
* @param the PARAM request parameter, the request parameter should be in the form of name1=value1&name2=value2.
* @return The response result of the remote resource represented
*/
public static string Sendpost (string url, string Param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realurl = new URL (url);
Opening and linking between URLs
URLConnection conn = Realurl.openconnection ();
To set common request properties
Conn.setrequestproperty ("accept", "*/*");
Conn.setrequestproperty ("connection", "keep-alive");
Conn.setrequestproperty ("user-agent",
"mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
To send a post request, you must set the following two lines
Conn.setdooutput (true);
Conn.setdoinput (true);
Gets the output stream corresponding to the URLConnection object
out = new PrintWriter (conn.getoutputstream ());
Send Request parameters
Out.print (param);
Buffer for flush output stream
Out.flush ();
Defines the response of the BufferedReader input stream to read the URL
in = new BufferedReader (
New InputStreamReader (conn.getinputstream ()));
String line;
While (line = In.readline ()) = null) {
Result + = line;
}
} catch (Exception E) {
System.out.println ("send POST Request Exception!") "+e);
E.printstacktrace ();
}
Use the finally block to close the output stream, input stream
finally{
try{
If (out!=null) {
Out.close ();
}
If (in!=null) {
In.close ();
}
}
Catch (ioexception Ex) {
Ex.printstacktrace ();
}
}
Return result;
}

/* public static void main (String Args[]) throws parserconfigurationexception, saxexception, ioexception{
String url= "http://www.hzjtydzs.com/web/xmlsvc/currentJamIndex.aspx?_=1482462966169";
String param = "";
String Filelac = Sendget (url, param);
Element AAA = Xmlhelper.getrootelement (filelac);
}*/
}

Sends a GET method to the specified URL to obtain a resource, encoding Problem.

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.