『壹』 怎么用java写一个http接口

一个servlet接口就可以了啊:

HTTP Header 请求实例

下面的实例使用 HttpServletRequest 的getHeaderNames()方法读取 HTTP 头信息。该方法返回一个枚举,包含与当前的 HTTP 请求相关的头信息。

一旦我们有一个枚举,我们可以以标准方式循环枚举,使用hasMoreElements()方法来确定何时停止,使用nextElement()方法来获取每个参数的名称。

//导入必需的java库
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.Enumeration;

importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;

@WebServlet("/DisplayHeader")

//扩展HttpServlet类
{

//处理GET方法请求的方法
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException
{
//设置响应内容类型
response.setContentType("text/html;charset=UTF-8");

PrintWriterout=response.getWriter();
Stringtitle="HTTPHeader请求实例-菜鸟教程实例";
StringdocType=
"<!DOCTYPEhtml> ";
out.println(docType+
"<html> "+
"<head><metacharset="utf-8"><title>"+title+"</title></head> "+
"<bodybgcolor="#f0f0f0"> "+
"<h1align="center">"+title+"</h1> "+
"<tablewidth="100%"border="1"align="center"> "+
"<trbgcolor="#949494"> "+
"<th>Header名称</th><th>Header值</th> "+
"</tr> ");

EnumerationheaderNames=request.getHeaderNames();

while(headerNames.hasMoreElements()){
StringparamName=(String)headerNames.nextElement();
out.print("<tr><td>"+paramName+"</td> ");
StringparamValue=request.getHeader(paramName);
out.println("<td>"+paramValue+"</td></tr> ");
}
out.println("</table> </body></html>");
}
//处理POST方法请求的方法
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
doGet(request,response);
}
}

『贰』 有没有类似java下httpclient的C++库,最好开源的,thx

用libcurl啊, 大名鼎鼎的curl底层所使用的库就是libcurl

『叁』 用java做一个httpClient 发送https 的get请求,需要证书验证的那种,求大神指点一下!

你那个 SSLSocketFactory(ks) 是自己的类?

你有用过 KeyManager.init (...)? 和 TrustManager.init(...) ?

想要在连接建立过程上交互式的弹出确认对话框来的话需要我们自己提供一个 KeyManager 和 TrustManager 的实现类,这有点复杂,你可以看一个 Sun 的 X509KeyManager 是怎么做的,默认地情况下它是从自动搜索匹配的 subject ,我们需要用自己提供的方式弹出确认的过程还不是全自动,另外一个账户可能有多个数字证书,比如支付宝我们就有多个签发时间不一样的数字证书,在连接建立时 IE 会提示我们选择其中的一个来使用,银行的 U 盾在安装多张数字证书时也会提示我们选择其中一个对应到你正在使用的银行卡号的那张证书。

『肆』 java怎样读取http文件服务器上的文件列表并下载

文件名不写死,可以用 File f = new File("存文件的目录");

调用 String f_names[] = f.list()显示该目录下的所有文件

根据路径和文件名就版可以得到相应的文权件链接了,也就可以将其下载下来了。

『伍』 java中有没有第三方包有HttpWebRequest类的

这个是dot net里的吧。

JDK有个功能有限的HttpURLConnection

Apache HttpComponents 提供了相近版的功能。(HttpClient, HttpAsyncClient)
https://hc.apache.org/index.html

还有个异权步的 AsyncHttpClient https://github.com/AsyncHttpClient/async-http-client

『陆』 JAVA 怎么实现HTTP的POST方式通讯,以及HTTPS方式传递

虽然在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK
库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common
下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。以下是简单的post例子:
String url = "http://www.newsmth.net/bbslogin2.php";
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = { new NameValuePair("id", "youUserName"),
new NameValuePair("passwd", "yourPwd") };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||
statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
return;
}

详情见:http://www.ibm.com/developerworks/cn/opensource/os-httpclient/

『柒』 客户端A上有个java程序,怎么访问B端的数据库,需要http协议协议放在哪

这看你的B端是怎么设计的了,可以使开放HTTP,webservice等。
B端作为服务端架设好后,A端直接调用B端就可以了

『捌』 除了HttpClient,Java还有什么类似HttpClient的技术

更直接一点吧,用jdk自带的urlconnection来实现,无需依赖其他库。下边的示例实现了post和get方法。
示例如下:

Java代码

publicclassHttpUtil{
="UTF-8";

publicstaticStringpost(Stringurl,Map<String,String>postParams){
HttpURLConnectioncon=null;
OutputStreamosw=null;
InputStreamins=null;
try{
con=(HttpURLConnection)newURL(url).openConnection();
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
if(null!=postParams){
con.setDoOutput(true);
StringpostParam=encodeParameters(postParams);
byte[]bytes=postParam.getBytes(CHARSET);

con.setRequestProperty("Content-Length",
Integer.toString(bytes.length));

osw=con.getOutputStream();
osw.write(bytes);
osw.flush();
}

intresCode=con.getResponseCode();
if(resCode<400){
ins=con.getInputStream();
}else{
ins=con.getErrorStream();
}
returnreadContent(ins);
}catch(IOExceptione){

}finally{

try{
if(osw!=null){
osw.close();
}
if(ins!=null){
ins.close();
}
}catch(IOExceptione){
e.printStackTrace();
}

}
returnnull;
}

publicstaticStringget(Stringurl){
HttpURLConnectioncon=null;
OutputStreamosw=null;
InputStreamins=null;
try{
con=(HttpURLConnection)newURL(url).openConnection();
con.setRequestMethod("GET");


intresCode=con.getResponseCode();
if(resCode<400){
ins=con.getInputStream();
}else{
ins=con.getErrorStream();
}
returnreadContent(ins);
}catch(IOExceptione){

}finally{

try{
if(osw!=null){
osw.close();
}
if(ins!=null){
ins.close();
}
}catch(IOExceptione){
e.printStackTrace();
}

}
returnnull;
}

(InputStreamins)throwsIOException{

StringBuildersb=newStringBuilder();
BufferedReaderbr=newBufferedReader(newInputStreamReader(ins,
HttpUtil.CHARSET));
if(ins!=null){
Stringline;
while((line=br.readLine())!=null){
sb.append(line);
}
}
returnsb.toString();
}

(Map<String,String>postParams){
StringBuilderbuf=newStringBuilder();
if(postParams!=null&&postParams.size()>0){

for(Map.Entry<String,String>tmp:postParams.entrySet()){
try{
buf.append(URLEncoder.encode(tmp.getKey(),CHARSET))
.append("=")
.append(URLEncoder.encode(tmp.getValue(),CHARSET))
.append("&");
}catch(java.io.){
}
}

buf.deleteCharAt(buf.length()-1);

}

returnbuf.toString();

}
}

『玖』 java 连接数据库问题: 我现在想连接一个数据库,但是数据库必须要使用http通道才可以连接.

你自己都说了 "必须要使用http通道才可以连接"
-_-!!!! 难道有人能突破这个特殊的限制 ?
如果能突破,那还能叫 "必须" 么?

『拾』 mysql连接数据库要使用http通道,java连接数据库时需要怎么连

下载驱动的包:我用的是mysql-connector-java-5.1.8-bin.jar,并让java工程加载这个jar。
从网上摘抄的代码:
public class DBHelper {
public static final String url = "jdbc:mysql://127.0.0.1/student";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "root";

public Connection conn = null;
public PreparedStatement pst = null;

public DBHelper(String sql) {
try {
Class.forName(name);//指定连接类型
conn = DriverManager.getConnection(url, user, password);//获取连接
pst = conn.prepareStatement(sql);//准备执行语句
} catch (Exception e) {
e.printStackTrace();
}
}