だから私はJavaに移植する必要があるC#でこの関数を持っています
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;
}
}