Two ways Java can send a url request to get a return value

  • 2020-04-01 03:09:42
  • OfStack

There are two ways to send a url request using Java and get the value returned by the server

The first method:


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
publicstaticStringsendUrlRequest(StringurlStr,Stringparam1,Stringparam2)throwsException{
StringtempStr=null;
HttpClienthttpclient=newDefaultHttpClient();
Propertiesproperties=newProperties();
HttpEntityentity=null;
StringxmlContent="";
try
{
//Set timeout
httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,20000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,20000);
//Encapsulates the parameters to be passed
List<NameValuePair>nvps=newArrayList<NameValuePair>();
nvps.add(newBasicNameValuePair("mainMemoCode",strmainMemoCode));
nvps.add(newBasicNameValuePair("recordPassWord",strrecordPassWord));
//The client requests the method type
HttpPosthttpPost=newHttpPost(urlStr);
httpPost.setEntity(newUrlEncodedFormEntity(nvps,"GBK"));
HttpResponseresponse=httpclient.execute(httpPost);
//Gets the value of the content-type that the server returns Http
tempStr=response.getHeaders("Content-Type")[0].getValue().toString();
//Gets the value of the page returned by the server
entity=response.getEntity();
xmlContent=EntityUtils.toString(entity);
Stringstrmessage=null;
System.out.println(xmlContent);
System.out.println(response.getHeaders("Content-Type")[0].getValue().toString());
httpPost.abort();
}
catch(SocketTimeoutExceptione)
{
}
catch(Exceptionex)
{
ex.printStackTrace();
}
finally{
httpclient.getConnectionManager().shutdown();
}

The second method:


publicstaticStringsendUrlRequest(StringurlStr,Stringparam1,Stringparam2)throwsException{
HttpURLConnectionurl_con=null;
try{
URLurl=newURL(urlStr);
StringBufferbankXmlBuffer=newStringBuffer();
//Create a URL connection, submit to the data, and get the results back
HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent","directclient");
PrintWriterout=newPrintWriter(newOutputStreamWriter(connection.getOutputStream(),"GBK"));
out.println(param);
out.close();
BufferedReaderin=newBufferedReader(newInputStreamReader(connection
.getInputStream(),"GBK"));
StringinputLine;
while((inputLine=in.readLine())!=null){
bankXmlBuffer.append(inputLine);
}
in.close();
tempStr=bankXmlBuffer.toString();
}
catch(Exceptione)
{
System.out.println(" send GET Request exception! "+e);
e.printStackTrace();
}finally{
if(url_con!=null)
url_con.disconnect();
}
returntmpeStr;
}


Related articles: