1. java如何调用对方http接口 新手虚心求教
importjava.io.BufferedReader;
importjava.io.DataOutputStream;
importjava.io.InputStreamReader;
importjava.net.HttpURLConnection;
importjava.net.URL;
importjava.net.URLEncoder;
publicclassDemoTest1{
publicstaticfinalStringGET_URL="http://112.4.27.9/mall-back/if_user/store_list?storeId=32";
//publicstaticfinalStringPOST_URL="http://112.4.27.9/mall-back/if_user/store_list";
//妙兜测试接口
publicstaticfinalStringPOST_URL="http://121.40.204.191:8180/mdserver/service/installLock";
/**
*接口调用GET
*/
(){
try{
URLurl=newURL(GET_URL);//把字符串转换为URL请求地址
HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();//打开连接
connection.connect();//连接会话
//获取输入流
BufferedReaderbr=newBufferedReader(newInputStreamReader(connection.getInputStream(),"UTF-8"));
Stringline;
StringBuildersb=newStringBuilder();
while((line=br.readLine())!=null){//循环读取流
sb.append(line);
}
br.close();//关闭流
connection.disconnect();//断开连接
System.out.println(sb.toString());
}catch(Exceptione){
e.printStackTrace();
System.out.println("失败!");
}
}
/**
*接口调用POST
*/
(){
try{
URLurl=newURL(POST_URL);
//将url以open方法返回的urlConnection连接强转为HttpURLConnection连接(标识一个url所引用的远程对象连接)
HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();//此时cnnection只是为一个连接对象,待连接中
//设置连接输出流为true,默认false(post请求是以流的方式隐式的传递参数)
connection.setDoOutput(true);
//设置连接输入流为true
connection.setDoInput(true);
//设置请求方式为post
connection.setRequestMethod("POST");
//post请求缓存设为false
connection.setUseCaches(false);
//设置该HttpURLConnection实例是否自动执行重定向
connection.setInstanceFollowRedirects(true);
//设置请求头里面的各个属性(以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)
//application/x-javascripttext/xml->xml数据application/x-javascript->json对象application/x-www-form-urlencoded->表单数据
//;charset=utf-8必须要,不然妙兜那边会出现乱码【★★★★★】
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
//建立连接(请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
connection.connect();
//创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)
DataOutputStreamdataout=newDataOutputStream(connection.getOutputStream());
Stringapp_key="app_key="+URLEncoder.encode("","utf-8");//已修改【改为错误数据,以免信息泄露】
Stringagt_num="&agt_num="+URLEncoder.encode("10111","utf-8");//已修改【改为错误数据,以免信息泄露】
Stringpid="&pid="+URLEncoder.encode("BLZXA150401111","utf-8");//已修改【改为错误数据,以免信息泄露】
Stringdepartid="&departid="+URLEncoder.encode("10007111","utf-8");//已修改【改为错误数据,以免信息泄露】
Stringinstall_lock_name="&install_lock_name="+URLEncoder.encode("南天大门","utf-8");
Stringinstall_address="&install_address="+URLEncoder.encode("北京育新","utf-8");
Stringinstall_gps="&install_gps="+URLEncoder.encode("116.350888,40.011001","utf-8");
Stringinstall_work="&install_work="+URLEncoder.encode("小李","utf-8");
Stringinstall_telete="&install_telete="+URLEncoder.encode("13000000000","utf-8");
Stringintall_comm="&intall_comm="+URLEncoder.encode("一切正常","utf-8");
//格式parm=aaa=111&bbb=222&ccc=333&ddd=444
Stringparm=app_key+agt_num+pid+departid+install_lock_name+install_address+install_gps+install_work+install_telete+intall_comm;
//将参数输出到连接
dataout.writeBytes(parm);
//输出完成后刷新并关闭流
dataout.flush();
dataout.close();//重要且易忽略步骤(关闭流,切记!)
//System.out.println(connection.getResponseCode());
//连接发起请求,处理服务器响应(从连接获取到输入流并包装为bufferedReader)
BufferedReaderbf=newBufferedReader(newInputStreamReader(connection.getInputStream(),"UTF-8"));
Stringline;
StringBuildersb=newStringBuilder();//用来存储响应数据
//循环读取流,若不到结尾处
while((line=bf.readLine())!=null){
//sb.append(bf.readLine());
sb.append(line).append(System.getProperty("line.separator"));
}
bf.close();//重要且易忽略步骤(关闭流,切记!)
connection.disconnect();//销毁连接
System.out.println(sb.toString());
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
//httpURLConectionGET();
httpURLConnectionPOST();
}
}
2. java如何读取http请求的全部内容
通过方法可以知道访问者的request.getHeader("Referer");
3. java怎样获取http请求的body
读取Body使用request.getReader(),但getReader获取的是BufferedReader,需要把它转换成字符串,下面是转换的方法
public class TestController {
@RequestMapping("/a")
protected void doPost(HttpServletRequest request,
HttpServletResponse response, BufferedReader br)
throws ServletException, IOException {
//Header部分
System.out.print(request.getHeaderNames());
Enumeration<?> enum1 = request.getHeaderNames();
while (enum1.hasMoreElements()) {
String key = (String) enum1.nextElement();
String value = request.getHeader(key);
System.out.println(key + "\t" + value);
}
//body部分
String inputLine;
String str = "";
try {
while ((inputLine = br.readLine()) != null) {
str += inputLine;
}
br.close();
} catch (IOException e) {
System.out.println("IOException: " + e);
}
System.out.println("str:" + str);
}
4. java怎样读取http文件服务器上的文件列表并下载
要求文件名不能写死,那么只能到服务器上去遍历目录,如果服务器开了ftp权限的话到可以用apache的commons-net包,里面有ftp功能可以上传下载文件,也可以遍历文件
5. java http协议如何获取响应内容
用XStream可以将一个java对象序列化成一个xml文件,然后通过http请求将该文件发送过去,然后在另一个网站也用XStream将java对象反序列化回来。反之也是同理。
6. 怎么用java读取一个http://。。。。。 .xml并返回json数据啊
可以用httpClient 发起一个 get或者post请求然后得到返回的结果再做json的解析即可
httpClient 用法:
1. GET 方式传递参数
//先将参数放入List,再对参数进行URL编码
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "数据")); //增加参数1
params.add(new BasicNameValuePair("param2", "value2"));//增加参数2
String param = URLEncodedUtils.format(params, "UTF-8");//对参数编码
String baseUrl = "服务器接口完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//将URL与参数拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //发起GET请求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式传递参数
//和GET方式一样,先将参数放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加参数1
params.add(new BasicNameValuePair("param2", "第二个参数"));//增加参数2
try {
HttpPost postMethod = new HttpPost(baseUrl);//创建一个post请求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //执行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
7. java 访问http
你的代码由问题吧。。。。。
1.创建连接:
URL url = new URL("http://www..com");
2.打开连接,获取连接输入流。
InputStream in = url.openConnection().getInputStream();
3.解析流。
System.out.println(IOUtils.toString(in));//输出访问地址内容。。。。
8. java如何调用对方http接口
你是指发送http请求吗,可以使用Apache 的 HttpClient
//构建HttpClient实例
CloseableHttpClienthttpclient=HttpClients.createDefault();//设置请求超时时间
RequestConfigrequestConfig=RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();//指定POST请求
HttpPosthttppost=newHttpPost(url);
httppost.setConfig(requestConfig);//包装请求体
List<NameValuePair>params=newArrayList<NameValuePair>();params.addAll(content);
HttpEntityrequest=newUrlEncodedFormEntity(params,"UTF-8");//发送请求
httppost.setEntity(request);
=httpclient.execute(httppost);//读取响应
HttpEntityentity=httpResponse.getEntity();Stringresult=null;if(entity!=null){
result=EntityUtils.toString(entity,"UTF-8");
}
9. 求助,Java中如何根据一个http接口获取JSON数据
用ajax,回调函数,function(data){};从data里拿到response的信息;接下来看你的业务了
10. Java中如何根据一个http接口获取JSON数据,http接口是通过第三方提供,有机构私钥,求代码示例
使用HttpClient,参照代码如下:
http://..com/question/1446921645999461940.html