所以我在 C# 中有这个函数,我需要移植到 JAVA
public static Response CallService(string strURL, string Token, int timeout, bool isPostMethod = true)
{
string error = "";
HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
try
{
if (strURL.Contains("https"))
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var httpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
if (isPostMethod)
httpWebRequest.Method = "POST";
else
httpWebRequest.Method = "GET";
httpWebRequest.ContentLength = 0;
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Timeout = timeout;
httpWebRequest.Headers["Token"] = HttpUtility.UrlEncode(Token, Encoding.UTF8);
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
statusCode = httpResponse.StatusCode;
return new Response(statusCode, httpResponse.GetResponseStream());
}
catch (Exception ex)
{
error = ex.Message;
if (!string.IsNullOrEmpty(error) && error.Contains("401"))
statusCode = HttpStatusCode.Unauthorized;
}
return new Response(statusCode, null, error);
}
这是我到目前为止所拥有的:
public static Response CallService(String strURL, String Token, int timeout, Boolean isPostMethod) {
String error = "";
int statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
try
{
URL url = new URL(strURL);
// Allow non trusted ssl certificates
if(strURL.startsWith("https"))
{
TrustManagerManipulator.allowAllSSL();
}
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (isPostMethod) {
urlConnection.setRequestMethod("POST");
}
else {
urlConnection.setRequestMethod("GET");
}
// Allow Inputs
urlConnection.setDoInput(true);
// Allow Outputs
urlConnection.setDoOutput(true);
// Don't use a cached copy.
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Token", Token);
urlConnection.setConnectTimeout(timeout);
statusCode = urlConnection.getResponseCode();
return new Response(statusCode, urlConnection.getInputStream(), "");
} catch (Exception ex) {
error = ex.getMessage();
if (error != null && !error.equals("") && error.contains("401"))
statusCode = HttpStatus.SC_UNAUTHORIZED;
} finally {
}
return new Response(statusCode, null, error);
}
我想知道我是否已成功移植该函数,因为每次执行 (urlConnection.getInputStream()) 时都会出错
这是 C# 中的 Response 类:
public class Response
{
private HttpStatusCode statusCode;
private Stream responseStream;
private string exception;
public HttpStatusCode StatusCode { get { return statusCode; } }
public Stream ResponseStream { get { return responseStream; } }
public string ExceptionError { get { return exception; } }
public Response(HttpStatusCode code, Stream stream, string strException = "")
{
this.statusCode = code;
this.responseStream = stream;
this.exception = strException;
}
}
这是 Java 中的移植响应类:
public static class Response
{
private int statusCode;
private InputStream responseStream;
private String exception;
public int getStatusCode() {
return statusCode;
}
public InputStream getResponseStream() {
return responseStream;
}
public String getExceptionError() {
return exception;
}
public Response(int code, InputStream stream, String strException)
{
this.statusCode = code;
this.responseStream = stream;
this.exception = strException;
}
}