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

热点内容
加密狗可以拔掉电脑上吗 浏览:97
rsa解密算法c语言实现 浏览:547
视觉目标跟踪算法研究 浏览:325
甘肃服务器中心云主机 浏览:891
遗传算法和蚁群算法的理解 浏览:603
51单板机编程汇编语言 浏览:505
新百度app怎么关闭无痕 浏览:670
程序员劳务派遣 浏览:611
加密java框架 浏览:719
硬盘这么把文件设置加密 浏览:414
linux怎么查看磁盘 浏览:348
已加密文件外网可以发吗 浏览:763
联通手机app怎么取消流量加油包 浏览:399
对话框文档可以多选加入文件夹吗 浏览:758
在修改表文件的结构时应使用什么命令 浏览:539
命令行如何与百度服务器连接 浏览:644
android44状态栏透明 浏览:682
华数频道加密 浏览:118
解压文件的密码怎么改 浏览:989
linuxlunch命令 浏览:273