java发送http请求
❶ 为什么使用java发送http请求
因为现在使用的是HTTP协议所以,SUN公司在servlet接口实现类的时候考虑到如果以后还有别的协议的话那么就会直接添加一个类来继承GenericServlet;
servlet是接口,GenericServlet是这个接口的实现类,
HttpServlet是GenericServlet的子类.
❷ java怎么主动发送http请求
实现思路就是先定义请求头内容,之后进行请求头设置。
定义请求头
给HttpPost 设置请求头
LinkedHashMap<String,String> headers = new LinkedHashMap<String,String>();
headers.put("Content-type","text/xml");
headers.put("Cache-Control", "no-cache");
headers.put("Connection", "close");
HttpPost httpPost = new HttpPost("地址");
if (headers != null) {
for (String key : headers.keySet()) {
httpPost.setHeader(key, headers.get(key));
}
}
备注:只需要在map中设置相应的请求头内容即可。根据实际需要修改即可
❸ 怎样java向服务器发送http请求
你好,java有一个组件,httpclient,这个jar包,可以模拟http客户端,向服务端发送http请求,而且现在大部分都用的是这个。
❹ JAVA在云服务器上发送http请求报错,跪求大神,小弟熬夜加班不容易啊
看你的url地址没写默认端口号,那么默认端口就是80端口,确认一下你云服务器web项目是否用的80端口,如果不是请在url里加入响应端口号
❺ java使用Http协议发请求报文
1.java.net.Socket 中有一个接口叫 PackageReceiver 你创建一个类继承自PackageReceiver
2.自己写一个类ConnectToHost 定义一些常量如
public ConnectToHost()
{
port = 0;
keepAlive = false;
lastCommTime = 0;
connectionCtrl = false;
maxConnection = 10;
connections = new ArrayList();
soTimeOut = 0;
inUsedConnection = 0;
alive = true;
al = true;
reconnectImmediately = false;
pollingWeight = 1;
}
3.写一个方法用来获取本地要发送的数据,如
private static String getRequestData(String name) throws IOException {
// TODO Auto-generated method stub
File testReq = new File("E:/virturlSend/", name);
// 输入流
FileInputStream infile = null;
InputStreamReader isr = null;
BufferedReader in = null;
String strLine = "";
StringBuffer strTotalLine = new StringBuffer();
try {
infile = new FileInputStream(testReq); // 以流的方式读配置文件
isr = new InputStreamReader(infile);
in = new BufferedReader(isr);
for (int iLine = 1; (strLine = in.readLine()) != null; iLine++) { // 从上传文件中每次读一行
// System.out.println(strLine.getBytes());
strTotalLine.append(strLine);
}
} catch (Exception ee) {
} finally {
in.close();
isr.close();
infile.close();
}
return strTotalLine.toString();
// return "baoyy";
}
4.设置主机信息 如
ConnectToHost connectToHost = new ConnectToHost();
//connectToHost.setHostAddr("20.*2.*.35");
//connectToHost.setPort(27098);
connectToHost.setHostAddr("127.*.*.1");
connectToHost.setPort(6789);
connectToHost.setMaxConnection(20);
5.发送数据
service.addConnectToHost(connectToHost);
以上代码不是全部 仅提供大体思路和构架 不知道能不能帮到您
❻ 怎么用java 发送http报文
URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL("http://211.147.222.30/se/sseS");
// 以post方式请求
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setConnectTimeout(6000);
httpurlconnection.setReadTimeout(6000);
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
msg=java.net.URLEncoder.encode(msg,"utf-8");
String username = "UserName=user&Password=pwd&SrcNumber=1065&DestTermID="
+ dest+ "&MsgContent=" + msg;
httpurlconnection.getOutputStream().write(
username.getBytes("utf-8"));
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
// 获取响应代码
code = httpurlconnection.getResponseCode();
// 获取页面内容
java.io.InputStream in = httpurlconnection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(
new InputStreamReader(in, "gb2312"));
String str = breader.readLine();
while (str != null) {
resp+=str;
str= breader.readLine();
}
} catch (Exception e) {
resp="err";
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
❼ 如何使用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请求500异常
是你请求的那个url服务出问题了 正常返回200状态码 但是服务返回500,请求的服务发生异常。去看看调用服务的日志
❾ Java开发中怎么向URL地址发送Http请求,根据返回的状态码判断成功失败
数据库服务器上不一定开了80端口呀,个人感觉判断网络是否通,还是使用Ping命令的好,如下:
String ip = "127.0.0.1"; // 使用正则,从Url中解析出数据库IP地址
System.out.println("ip = " + ip);
Process process = Runtime.getRuntime().exec("ping " + ip); // 执行Ping命令
DataInputStream dis = new DataInputStream(process.getInputStream());
byte[] buffer = new byte[1024 * 1000];
int len = dis.read(buffer);
StringBuffer sb = new StringBuffer();
while (len > 0) {
sb.append(new String(buffer, 0, len));
len = dis.read(buffer);
}
System.out.println("ping result = " + sb.toString());
// 从Ping的结果中,解析出丢失率
Pattern p = Pattern.compile("(?<=\\().*%");
Matcher m = p.matcher(sb.toString());
boolean flag = true; // 网络是否通
if (m.find()) {
String str = m.group();
System.out.println("lost = " + str);
// 如果丢失率等于100%,则说明网络不通
flag = str.equals("100%") ? false : true;
}
System.out.println("the net is " + flag);
❿ JAVA代码发送HTTP请求问题(我想实现和服务器进行一次连接时发送两次请求)
我觉得你这个问题的解决应该是你的程序做一次这个网站的登陆,而且这个登陆的动作应该是需要发生在你这段代码以前,因为你这段代码的动作其实只是访问了一下那个网站,但是没有任何的用户或者是其他的信息。
一般来说你登陆以后,你会获得一个token,用那个token就可以让网站认为你已经登陆,然后改密码什么就好办了。建议你先抓一下IE的包看看是人家的通信是怎么样的,然后用java做就好了。或者是那个网站有开发者文档就最好了。