5个回答
展开全部
WebService很简单,无论你想发布服务还是调取别人的服务。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java中的servlet
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import 察滚java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class JavaServletSample extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO
}
public void doPost(HttpServletRequest request,
宏没仿 蔽纤 HttpServletResponse response) throws ServletException, IOException {
// TODO
}
}
最简单的一种。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package com.huaye.network.util;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
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;
public class HttpUtil {
/**
* http������ݣ�Ĭ�Ϸ��ص����ΪUTF-8����
*
* @param url
* url��ַ
* @param method
* �ύ��ʽ
* @return
* @throws IOException
*/蔽岩袭
public static String getUrl(String url) throws IOException {
try {
HttpGet request = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); // ��������ʱʱ��
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); // ��ȡ��ʱ宏兄
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
//httpClient.getConnectionManager().releaseConnection(conn, validDuration, timeUnit)
return result;
} else {
return "kong";
}
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
e.printStackTrace();
return "kong";
}
}
/**
* post�ύ��
*
* @param url
* �ύ��ַ枣毁
* @param params
* ����
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String postUrl(String url, Map<String, String> params)
throws ClientProtocolException, IOException {
return postUrl(url, params, "UTF-8");
}
/**
* post�ύ��
*
* @param url
* �ύ��ַ
* @param params
* ����
* @param encoding
* ������
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String postUrl(String url, Map<String, String> params,
String encoding) throws ClientProtocolException, IOException {
List<NameValuePair> param = new ArrayList<NameValuePair>(); // ����
// param.add(new BasicNameValuePair("par", "request-post"));
// //��������Ҳ�֪���Dz��Ƿ�Ҫ�����
// ��Ӳ��
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
HttpPost request = new HttpPost(url);
HttpEntity entity = new UrlEncodedFormEntity(param, encoding);
request.setEntity(entity);
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); // ��������ʱʱ��
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); // ��ȡ��ʱ
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity(), "utf-8");
} else {
return "null";
}
}
}
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
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;
public class HttpUtil {
/**
* http������ݣ�Ĭ�Ϸ��ص����ΪUTF-8����
*
* @param url
* url��ַ
* @param method
* �ύ��ʽ
* @return
* @throws IOException
*/蔽岩袭
public static String getUrl(String url) throws IOException {
try {
HttpGet request = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); // ��������ʱʱ��
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); // ��ȡ��ʱ宏兄
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
//httpClient.getConnectionManager().releaseConnection(conn, validDuration, timeUnit)
return result;
} else {
return "kong";
}
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
e.printStackTrace();
return "kong";
}
}
/**
* post�ύ��
*
* @param url
* �ύ��ַ枣毁
* @param params
* ����
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String postUrl(String url, Map<String, String> params)
throws ClientProtocolException, IOException {
return postUrl(url, params, "UTF-8");
}
/**
* post�ύ��
*
* @param url
* �ύ��ַ
* @param params
* ����
* @param encoding
* ������
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String postUrl(String url, Map<String, String> params,
String encoding) throws ClientProtocolException, IOException {
List<NameValuePair> param = new ArrayList<NameValuePair>(); // ����
// param.add(new BasicNameValuePair("par", "request-post"));
// //��������Ҳ�֪���Dz��Ƿ�Ҫ�����
// ��Ӳ��
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
HttpPost request = new HttpPost(url);
HttpEntity entity = new UrlEncodedFormEntity(param, encoding);
request.setEntity(entity);
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); // ��������ʱʱ��
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); // ��ȡ��ʱ
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity(), "utf-8");
} else {
return "null";
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是想调用别人接口,还是接口被人调用 ?
追问
被别人调用
追答
很简单啊,比如直接写个servlet就行,接收参数可以是json什么的,返回json也行!访问地址就是你的项目地址+配置的servlet名称+参数。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询