導航:首頁 > 編程語言 > javaurltostring

javaurltostring

發布時間:2023-08-28 22:14:10

A. java如何將字元串轉化為URL

將字元串轉換成URL可以使用創建一個URL對象,並將字元串賦給這個URL對象。
參考代碼如下:

String str = "填寫字元串的鏈接地址";
try {
URL url = new URL(str);
} catch (MalformedURLException e) {
e.printStackTrace();
}
注意,創建URL對象會有異常,所以使用try處理拋出的異常。

B. 用java做分頁時,如何獲取url

httpServletRequest.getRequestURI();
或者
string url = HttpContext.Current.Request.Url.ToString();
//獲取當前頁的URL
或者
設置參數 前台傳參:....&url=.... 後台接值: request.getParameter("url")
java 後台分頁 httpServletRequest.getRequestURI();
建議用AJAX,這樣可以不需要獲取 url

C. java獲取伺服器文件,怎樣用url返回

下面提供二種方法會使用java發送url請求,並獲取伺服器返回的值

第一種方法:
代碼如下:

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;

(StringurlStr,Stringparam1,Stringparam2)throwsException{
StringtempStr=null;
HttpClienthttpclient=newDefaultHttpClient();
Propertiesproperties=newProperties();
HttpEntityentity=null;
StringxmlContent="";
try
{

//設置超時時間
httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,20000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,20000);

//封裝需要傳遞的參數
List<NameValuePair>nvps=newArrayList<NameValuePair>();
nvps.add(newBasicNameValuePair("mainMemoCode",strmainMemoCode));
nvps.add(newBasicNameValuePair("recordPassWord",strrecordPassWord));
//客戶端的請求方法類型
HttpPosthttpPost=newHttpPost(urlStr);
httpPost.setEntity(newUrlEncodedFormEntity(nvps,"GBK"));
HttpResponseresponse=httpclient.execute(httpPost);

//獲取伺服器返回Http的Content-Type的值
tempStr=response.getHeaders("Content-Type")[0].getValue().toString();

//獲取伺服器返回頁面的值
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();
}
第二種方法:

代碼如下:


(StringurlStr,Stringparam1,Stringparam2)throwsException{

HttpURLConnectionurl_con=null;
try{
URLurl=newURL(urlStr);
StringBufferbankXmlBuffer=newStringBuffer();
//創建URL連接,提交到數據,獲取返回結果
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("發送GET請求出現異常!"+e);
e.printStackTrace();

}finally{
if(url_con!=null)
url_con.disconnect();
}

returntmpeStr;
}

總結:多練習代碼,熟練之後才能更快速的去了解代碼的學習的方法。多去獲取一些思維方面的書籍可以看看。

D. JAVA URL類中的這個構造函數的參數是什麼意思

context是用來保存基路徑的,新建的URL的路徑會是context中的路徑和spec拼接起來的路徑。
比如
URL baseURL=new URL("http://www..com");
URL url=new URL(baseURL,"img/bd_logo1.png");
String path=url.toString();//path為"www..com/img/bd_logo1.png"

E. 用java怎麼寫URL介面

在java中,調用http請求介面,主要通過流的方式進行調用,示例介面如下:
/**
* 程序中訪問http數據介面
*/
public String searchLoginService(String urlStr) {

/** 網路的url地址 */
URL url = null;

/** http連接 */
HttpURLConnection httpConn = null;

/**//** 輸入流 */
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try{
url = new URL(urlStr);
in = new BufferedReader( new InputStreamReader(url.openStream(),"UTF-8") );
String str = null;
while((str = in.readLine()) != null) {
sb.append( str );
}
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
} finally{
try{
if(in!=null) {
in.close();
}
}catch(IOException ex) {
logger.error(ex.getMessage(), ex);
}
}
String result =sb.toString();
System.out.println(result);
return result;
}

F. java如何獲取網頁中的文字

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

public class URLTest {
// 一個public方法,返回字元串,錯誤則返回"error open url"
public static String getContent(String strUrl) {
try {
URL url = new URL(strUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url
.openStream()));
String s = "";
StringBuffer sb = new StringBuffer("");
while ((s = br.readLine()) != null) {
sb.append(s + "/r/n");
}
br.close();
return sb.toString();
} catch (Exception e) {
return "error open url:" + strUrl;
}
}

public static void initProxy(String host, int port, final String username,
final String password) {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
new String(password).toCharArray());
}
});
System.setProperty("http.proxyType", "4");
System.setProperty("http.proxyPort", Integer.toString(port));
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxySet", "true");
}

public static void main(String[] args) throws IOException {
String url = "https://www.jb51.net";
String proxy = "http://192.168.22.81";
int port = 80;
String username = "username";
String password = "password";
String curLine = "";
String content = "";
URL server = new URL(url);
initProxy(proxy, port, username, password);
HttpURLConnection connection = (HttpURLConnection) server
.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
while ((curLine = reader.readLine()) != null) {
content = content + curLine+ "/r/n";
}
System.out.println("content= " + content);
is.close();
System.out.println(getContent(url));
}
}

閱讀全文

與javaurltostring相關的資料

熱點內容
訂購命令英語 瀏覽:659
java正則網址 瀏覽:777
程序員上班可不可以自學 瀏覽:428
空調壓縮機排空氣視頻 瀏覽:283
centos72nginxphp 瀏覽:184
游戲平台用什麼伺服器好 瀏覽:753
保密櫃里的圖片是加密文件嗎 瀏覽:909
php判斷最後一個字元 瀏覽:635
pdf腦區 瀏覽:635
at命令已棄用 瀏覽:490
買點賣出指標源碼 瀏覽:612
36位單片機 瀏覽:428
英雄聯盟山東伺服器地址 瀏覽:213
sd伺服器什麼意思 瀏覽:617
thinkphp去indexphp 瀏覽:62
電腦顯示連接未加密 瀏覽:193
zao伺服器怎麼修改 瀏覽:245
php使用jsapi調起支付 瀏覽:891
vivo雲伺服器網 瀏覽:723
cmd遠程連接命令行 瀏覽:961