导航:首页 > 编程语言 > 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相关的资料

热点内容
第二届程序员节开幕式 浏览:82
运维程序员脚本 浏览:369
塑源码燕窝的安全性 浏览:174
作业调度采用高响应比优先调度算法 浏览:160
和平精英如何切换苹果到安卓 浏览:530
数据库调用表单的命令 浏览:920
程序员技能大赛 浏览:9
什么app买品牌衣服 浏览:13
手机看世界名着哪个app好 浏览:493
运行命令切换打印机用户 浏览:919
android滑动button 浏览:939
服务器日志可以干什么 浏览:644
安卓系统空间怎么清理软件 浏览:343
维也纳pdf 浏览:641
加密货币交易所哪个最好 浏览:816
linux的现状 浏览:926
命令与征服叛逆者修改器 浏览:246
怎么用ios玩安卓全民枪战 浏览:668
程序员入行前后的头发 浏览:711
嵌入式图像算法 浏览:329