当前位置:首页 » 密码管理 » 访问url获取返回值

访问url获取返回值

发布时间: 2023-07-28 13:56:47

php中怎么获取一个URL的返回数据

http get一下就返回源码了。。比如 url为 target/XX.PHP?V=1
用游览器访问直接下载一个MP3 可以参考如下代码:
<?php
$data = file_get_contents('target/XX.PHP?V=1');
file_put_contents('test.mp3', $data, true);
?>

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;
}

总结:多练习代码,熟练之后才能更快速的去了解代码的学习的方法。多去获取一些思维方面的书籍可以看看。

热点内容
手机号序列码的密码在哪里 发布:2025-02-02 03:29:34 浏览:873
安卓怎么换回鸿蒙系统 发布:2025-02-02 03:24:35 浏览:507
完美国际邻水镇箱子密码是多少 发布:2025-02-02 03:17:04 浏览:618
测试java程序 发布:2025-02-02 03:16:49 浏览:888
android罗升阳 发布:2025-02-02 03:15:01 浏览:822
javascript编程语言 发布:2025-02-02 03:05:49 浏览:360
用电账号初始密码多少 发布:2025-02-02 03:04:03 浏览:107
python赋值运算符 发布:2025-02-02 03:00:51 浏览:905
怎么查询电脑ip地址和dns服务器 发布:2025-02-02 02:57:50 浏览:240
数据库应用系统的概念 发布:2025-02-02 02:44:46 浏览:549