Java HTTPS-Web请求和响应是否已加密?
我正在从android应用程序向HTTPS服务器调用webservice。下面是代码片段,通过它我可以成功地进行web服务调用并获得响应 我的问题是,在调用HTTPS服务器之前,我们是否需要执行任何额外的步骤来加密数据? 因为,通过android profiler,我能够以纯文本格式查看我的所有Web请求。我的理解是,在进行HTTPS调用之前,请求将被加密Java HTTPS-Web请求和响应是否已加密?,java,android,http,https,ssl-certificate,Java,Android,Http,Https,Ssl Certificate,我正在从android应用程序向HTTPS服务器调用webservice。下面是代码片段,通过它我可以成功地进行web服务调用并获得响应 我的问题是,在调用HTTPS服务器之前,我们是否需要执行任何额外的步骤来加密数据? 因为,通过android profiler,我能够以纯文本格式查看我的所有Web请求。我的理解是,在进行HTTPS调用之前,请求将被加密 publicstaticwebservicesp makeWebServiceCall(stringxml,stringurlpath)抛
publicstaticwebservicesp makeWebServiceCall(stringxml,stringurlpath)抛出IOException{
//用于发出web服务HTTP请求的代码
字符串responseString=“”;
字符串outputString=“”;
字符串wsURL=urlPath;
URL=新URL(wsURL);
URLConnection=url.openConnection();
HttpsURLConnection httpConn=(HttpsURLConnection)连接;
ByteArrayOutputStream bout=新建ByteArrayOutputStream();
//System.out.println(XML);
byte[]buffer=新字节[XML.length()];
buffer=XML.getBytes();
写(缓冲区);
字节[]b=bout.toByteArray();
//设置适当的HTTP参数。
httpConn.setRequestProperty(“内容长度”,
String.valueOf(b.length));
setRequestProperty(“内容类型”,“text/xml;charset=utf-8”);
httpConn.setRequestMethod(“POST”);
setRequestProperty(“缓存控制”、“无缓存”);
httpConn.setDoOutput(真);
httpConn.setDoInput(真);
OutputStream out=httpConn.getOutputStream();
//将请求的内容写入HTTP连接的outputstream。
写出(b);
out.close();
//已准备好发送请求。
//检查状态
int status=httpConn.getResponseCode();
Log.d(标记“makeWebServiceCall:+”处理状态:+状态);
缓冲读取器;
如果(状态否。如果您将其发送到https网站,则加密是作为协议的一部分完成的。您不需要做任何额外的工作。否。您看到的加密在网络层。发起https呼叫的客户端可以查看发送的内容和接收的内容。https就是这样工作的
当你查看chrome浏览器的“网络”选项卡时,你会看到发送了什么和接收了什么。现在这不是一个安全问题,https更多的是关于你所做的事情,这使得网络之间的任何人都很难窃听你的数据
现在,如果您仍然需要额外的安全级别,可以使用证书固定
因此,在这种技术中,您基本上说您期望的证书哈希包含此内容。然后,如果有人在系统上使用具有可信CA的可信代理,即使为给定域生成了有效证书,也不会建立连接。HTTPS对您的应用程序是透明的,所有gic发生在传输层之间(因此称为“传输层安全”),你可以想象过去加密的电报,将军们用纯文本告诉电报员消息,电报员以加密的形式发送(可能使用某种码本),任何没有相同码本的人都无法轻松解密消息,任何使用电报的人都不关心码本(甚至不知道它,除了“传输层”两侧的电报员).加密/解密由操作系统提供的内置网络客户端模块完成。因此,您无需担心
您可以使用一些客户端工具查看纯文本,因为它们确切地知道发送/接收的内容。例如,chrome developer工具。(实际上它们也不关心加密/解密).但从Android studio,我可以看到我的应用程序以纯XML格式发出的所有请求?这是Android studio在移动设备连接到调试模式时可以解密的吗?在Android studio中的何处?你在看什么?它在发送到HTTP堆栈深处的操作系统套接字子系统之前不会加密。添加了d的图像我可以在纯文本中看到的Web请求的加密形式是的,在堆栈的更高级别上,在加密之前/解密之后。如果你在路由器上嗅探流量,它将被加密。
public static WebServiceResp makeWebServiceCall(String XML, String urlPath) throws IOException{
//Code to make a web service HTTP request
String responseString = "";
String outputString = "";
String wsURL = urlPath;
URL url = new URL(wsURL);
URLConnection connection = url.openConnection();
HttpsURLConnection httpConn = (HttpsURLConnection)connection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
//System.out.println(XML);
byte[] buffer = new byte[XML.length()];
buffer = XML.getBytes();
bout.write(buffer);
byte[] b = bout.toByteArray();
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty("Content-Length",
String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Cache-Control", "no-cache");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
//Write the content of the request to the outputstream of the HTTP Connection.
out.write(b);
out.close();
//Ready with sending the request.
//Check the status
int status = httpConn.getResponseCode();
Log.d(TAG, "makeWebServiceCall: "+"Processing Status: "+status);
BufferedReader in;
if (status <= 200) {
//Read the response.
Log.d(TAG, "makeWebServiceCall: Getting Input Stream");
InputStreamReader isr =
new InputStreamReader(httpConn.getInputStream());
in = new BufferedReader(isr);
}else{
//Read the response.
Log.d(TAG, "makeWebServiceCall: Getting Error Stream");
InputStreamReader isr =
new InputStreamReader(httpConn.getErrorStream());
in = new BufferedReader(isr);
}
//Write the SOAP message response to a String.
while ((responseString = in.readLine()) != null) {
outputString = outputString + responseString;
}
Log.d(TAG, "makeWebServiceCall: WebServiceResponse " + outputString);
//Parse the String output to a org.w3c.dom.Document and be able to reach every node with the org.w3c.dom API.
Document document = Utils.parseXmlFile(outputString);
//NodeList nodeLst = document.getElementsByTagName("GetWeatherResult");
// String weatherResult = nodeLst.item(0).getTextContent();
//System.out.println("Weather: " + weatherResult);
//Write the SOAP message formatted to the console.
WebServiceResp webServiceResp = new WebServiceResp();
webServiceResp.setDocument(document);
webServiceResp.setStatus(status);
return webServiceResp;
}