当前位置:首页 » 密码管理 » java访问http请求

java访问http请求

发布时间: 2023-03-02 14:20:54

‘壹’ java HTTP请求 处理

javax.servlet.http.HttpResponse类用于产生返回页面.通过HttpResponse定义的方法getOutputStream()可以获得ServletOutputStream的实例,这样用户就可以利用ServletOutputStream.write方法向输出流中写入返回页面的内容. 但是ServletOutputStream使用的是缺省的编码方式,如果要使返回页面中的中文字 符能够正常显示,最好显示地指定所用的字符编码方式. 通常需要构造一个 OutputStreamWriter , 例程如下:

public void doGet (HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException

{

res.setContentType("text/html");

ServletOutputStream out = res.getOutputStream();

OutputStreamWriter ow = new OutputStreamWriter(out,"GB2312");

ow.write("这是测试");

ow.flush();

ow.close();

}

‘贰’ JAVA 使用Socket 访问HTTP服务器

你好。访问Web服务器(例:www..com),需要根据Http协议发送相关请求头,否则Web服务器不会处理。这种处理就类似于你的代码:(line = read.readLine()) != null。当然Web服务器验证的不是这个。

HTTP协议这里不会详解,有需要可以查询相关资料。

在你的代码中加入:

longstart=System.currentTimeMillis();
SocketAddressaddress=newInetSocketAddress(InetAddress.getByName("www.javathinker.org").getHostAddress(),80);
Socketsocket=newSocket();
socket.connect(address);
System.out.println("连接成功-"+socket.getInetAddress()+":"
+(System.currentTimeMillis()-start)+"ms");
/**编写简单HTTP请求头*/
StringBuildersb=newStringBuilder();
sb.append("GET/HTTP/1.1 ")
.append("Host:www.javathinker.org ")
.append("Connection:Close ");

OutputStreamout=socket.getOutputStream();
out.write(sb.toString().getBytes());
out.flush();
/**发送完成后flush*/
BufferedReaderread=newBufferedReader(newInputStreamReader(
socket.getInputStream()));
/**循环等待read接收*/
booleanb=true;
while(b){
if(read.ready()){
Stringline=null;
while((line=read.readLine())!=null){
System.out.println(line);
}
b=false;
}
}

‘叁’ java 怎么接收http请求

用servlet接收。
具体步骤是写一个类继承HttpServlet,如果是接收get请求就重写doGet(HttpServletRequest,HttpServletResponse),接收post就重写doPost(HttpServletRequest,HttpServletResponse),共同处理post和get就重写service(HttpServletRequest,HttpServletResponse)
其次在web.xml定义servlet标签,以及你这个servlet要处理的请求mapping
最后把项目部署在tomcat之类的web容器中即可。
如果使用框架的话就另当别论了,比如spring 的DispatcherServlet。当然你也可以自己写servlet。

‘肆’ 如何在java中发起http和https请求

1.写http请求方法
[java] view plain

//处理http请求 requestUrl为请求地址 requestMethod请求方式,值为"GET"或"POST"
public static String httpRequest(String requestUrl,String requestMethod,String outputStr){
StringBuffer buffer=null;
try{
URL url=new URL(requestUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(requestMethod);
conn.connect();
//往服务器端写内容 也就是发起http请求需要带的参数
if(null!=outputStr){
OutputStream os=conn.getOutputStream();
os.write(outputStr.getBytes("utf-8"));
os.close();
}
//读取服务器端返回的内容
InputStream is=conn.getInputStream();
InputStreamReader isr=new InputStreamReader(is,"utf-8");
BufferedReader br=new BufferedReader(isr);
buffer=new StringBuffer();
String line=null;
while((line=br.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}
return buffer.toString();
}

‘伍’ 如何使用HttpClient包实现JAVA发起HTTP请求

publicclassHttpClientUtil{

publicstaticStringdoGet(Stringurl,Map<String,String>param){

//创建Httpclient对象
CloseableHttpClienthttpclient=HttpClients.createDefault();


StringresultString="";
CloseableHttpResponseresponse=null;
try{
//创建uri
URIBuilderbuilder=newURIBuilder(url);
if(param!=null){
for(Stringkey:param.keySet()){
builder.addParameter(key,param.get(key));
}
}
URIuri=builder.build();

//创建httpGET请求
HttpGethttpGet=newHttpGet(uri);

//执行请求
response=httpclient.execute(httpGet);
//判断返回状态是否为200
if(response.getStatusLine().getStatusCode()==200){
resultString=EntityUtils.toString(response.getEntity(),"UTF-8");
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(response!=null){
response.close();
}
httpclient.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
returnresultString;
}

publicstaticStringdoGet(Stringurl){
returndoGet(url,null);
}

publicstaticStringdoPost(Stringurl,Map<String,String>param){
//创建Httpclient对象
CloseableHttpClienthttpClient=HttpClients.createDefault();
CloseableHttpResponseresponse=null;
StringresultString="";
try{
//创建HttpPost请求
HttpPosthttpPost=newHttpPost(url);
//创建参数列表
if(param!=null){
List<NameValuePair>paramList=newArrayList<>();
for(Stringkey:param.keySet()){
paramList.add(newBasicNameValuePair(key,param.get(key)));
}
//模拟表单
UrlEncodedFormEntityentity=newUrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
//执行http请求
response=httpClient.execute(httpPost);

System.out.println(response.getStatusLine());

resultString=EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(resultString);

}catch(Exceptione){
e.printStackTrace();
}finally{
try{
response.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

returnresultString;
}

publicstaticStringdoPost(Stringurl){
returndoPost(url,null);
}

publicstaticStringdoPostJson(Stringurl,Stringjson){
//创建Httpclient对象
CloseableHttpClienthttpClient=HttpClients.createDefault();
CloseableHttpResponseresponse=null;
StringresultString="";
try{
//创建HttpPost请求
HttpPosthttpPost=newHttpPost(url);
//创建请求内容
StringEntityentity=newStringEntity(json,ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
//执行http请求
response=httpClient.execute(httpPost);
resultString=EntityUtils.toString(response.getEntity(),"utf-8");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
response.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

returnresultString;
}
}

‘陆’ java 怎么接收http请求

你需要搭建一个服务器,才能接受http请求。

‘柒’ java 接受http请求

使用servlet


public class Test extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public Test() {

super();

// TODO Auto-generated constructor stub

}


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//接收get请求

// 这里写你接收request请求后要处理的操作

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//接收post请求

// 这里写你接收request请求后要处理的操作

}


}


‘捌’ JAVA代码发送HTTP请求问题(我想实现和服务器进行一次连接时发送两次请求)

我觉得你这个问题的解决应该是你的程序做一次这个网站的登陆,而且这个登陆的动作应该是需要发生在你这段代码以前,因为你这段代码的动作其实只是访问了一下那个网站,但是没有任何的用户或者是其他的信息。
一般来说你登陆以后,你会获得一个token,用那个token就可以让网站认为你已经登陆,然后改密码什么就好办了。建议你先抓一下IE的包看看是人家的通信是怎么样的,然后用java做就好了。或者是那个网站有开发者文档就最好了。

热点内容
qq缓存文件夹在哪里 发布:2025-08-21 08:31:14 浏览:615
python数组中文 发布:2025-08-21 08:29:42 浏览:764
挂号源码 发布:2025-08-21 08:17:06 浏览:184
ip如何登录服务器 发布:2025-08-21 08:17:03 浏览:985
小的压缩机 发布:2025-08-21 08:11:48 浏览:125
精易助手源码 发布:2025-08-21 08:10:24 浏览:842
mysql更新存储过程 发布:2025-08-21 08:04:52 浏览:221
数据库抓取 发布:2025-08-21 07:31:04 浏览:333
编程电脑租 发布:2025-08-21 07:26:48 浏览:248
ftp移动宽带 发布:2025-08-21 07:26:02 浏览:943