Java Code Examples for java.net.HttpURLConnection#setUseCaches()

The following examples show how to use java.net.HttpURLConnection#setUseCaches() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpRequest.java    From fc-java-sdk with MIT License 6 votes vote down vote up
public HttpURLConnection getHttpConnection(String urls, String method)
    throws IOException {
    String strUrl = urls;
    if (null == strUrl || null == method) {
        return null;
    }
    URL url = new URL(strUrl);

    System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setRequestMethod(method);
    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    httpConn.setUseCaches(false);
    httpConn.setConnectTimeout(Const.CONNECT_TIMEOUT);
    httpConn.setReadTimeout(Const.READ_TIMEOUT);

    for (Map.Entry<String, String> entry : headers.entrySet()) {
        httpConn.setRequestProperty(entry.getKey(), entry.getValue());
    }

    return httpConn;
}
 
Example 2
Source File: HurlStack.java    From TitanjumNote with Apache License 2.0 6 votes vote down vote up
/**
 * Opens an {@link HttpURLConnection} with parameters.
 * @param url
 * @return an open connection
 * @throws IOException
 */
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
    HttpURLConnection connection = createConnection(url);

    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    connection.setDoInput(true);

    // use caller-provided custom SslSocketFactory, if any, for HTTPS
    if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
        ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
    }

    return connection;
}
 
Example 3
Source File: OkHttpStack.java    From something.apk with MIT License 6 votes vote down vote up
/**
 * Opens an {@link HttpURLConnection} with parameters.
 * @param url
 * @return an open connection
 * @throws IOException
 */
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
    HttpURLConnection connection = createConnection(url);

    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    connection.setDoInput(true);

    // use caller-provided custom SslSocketFactory, if any, for HTTPS
    if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
        ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
    }

    return connection;
}
 
Example 4
Source File: TCPMsgSender.java    From java-tcp-tunnel with MIT License 6 votes vote down vote up
public static String sendGet(String to) throws Exception {
  HttpURLConnection conn = (HttpURLConnection) new URL(to).openConnection();
  conn.setRequestMethod("GET");

  conn.setRequestProperty("User-Agent", "tcptunnel-tester");

  conn.setUseCaches(false);
  conn.setAllowUserInteraction(false);

  int responseCode = conn.getResponseCode();
  String responseMsg = conn.getResponseMessage();

  StringBuilder response = new StringBuilder();
  InputStream in = conn.getInputStream();
  BufferedReader bin = new BufferedReader(new InputStreamReader(in));
  String line = "";
  while ((line = bin.readLine()) != null) {
    response.append(line);
  }
  conn.disconnect();
  //System.out.println(ln+"response:" + response.toString());
  return response.toString();
}
 
Example 5
Source File: TCPMsgSender.java    From java-tcp-tunnel with MIT License 6 votes vote down vote up
public static byte[] sendGZGet(String to) throws Exception {
  HttpURLConnection conn = (HttpURLConnection) new URL(to).openConnection();
  conn.setRequestMethod("GET");

  conn.setRequestProperty("User-Agent", "tcptunnel-tester");
  conn.setRequestProperty("Accept-Encoding", "gzip");

  conn.setUseCaches(false);
  conn.setAllowUserInteraction(false);

  int responseCode = conn.getResponseCode();
  String responseMsg = conn.getResponseMessage();

  InputStream in = conn.getInputStream();
  byte[] bytes = readBinaryStream(in);
  conn.disconnect();
  //System.out.println(ln+"response:" + response.toString());
  return bytes;
}
 
Example 6
Source File: B6401598.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {

                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

                httpURLConnection.setConnectTimeout(40000);
                httpURLConnection.setReadTimeout(timeout);
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setUseCaches(false);
                httpURLConnection.setAllowUserInteraction(false);
                httpURLConnection.setRequestMethod("POST");

                // HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);

                return httpURLConnection;
        }
 
Example 7
Source File: HttpUtil.java    From game-server with MIT License 5 votes vote down vote up
/**
 * GET METHOD
 *
 * @param strUrl String
 * @param timeout a int.
 * @throws java.io.IOException
 * @return List
 */
public static ByteArrayOutputStream URLGet(String strUrl, int timeout) throws IOException {
    URL url = new URL(strUrl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    InputStream is = null;
    ByteArrayOutputStream bos = null;
    try {
        con.setUseCaches(false);
        con.setConnectTimeout(timeout);// jdk 1.5换成这个,连接超时
        con.setReadTimeout(timeout);// jdk 1.5换成这个,读操作超时
        HttpURLConnection.setFollowRedirects(true);
        int responseCode = con.getResponseCode();
        if (HttpURLConnection.HTTP_OK == responseCode) {
            byte[] buffer = new byte[512];
            int len = -1;
            is = con.getInputStream();
            bos = new ByteArrayOutputStream();
            while ((len = is.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            return bos;

        }
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage(), ex);
    } finally {
        con.disconnect();
        if(is!=null){
        	is.close();
        }
    }
    return bos;
}
 
Example 8
Source File: HttpUtil.java    From game-server with MIT License 5 votes vote down vote up
/**
 * 充值相关的HTTP请求使用 GET METHOD
 *
 * @param strUrl String
 * @throws java.io.IOException
 * @return List
 */
public static ByteArrayOutputStream URLGetByRecharge(String strUrl) throws IOException {
    URL url = new URL(strUrl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    InputStream is = null;
    ByteArrayOutputStream bos = null;
    try {
        con.setUseCaches(false);
        con.setConnectTimeout(7000);// jdk 1.5换成这个,连接超时
        con.setReadTimeout(7000);// jdk 1.5换成这个,读操作超时
        HttpURLConnection.setFollowRedirects(true);
        int responseCode = con.getResponseCode();
        if (HttpURLConnection.HTTP_OK == responseCode) {
            byte[] buffer = new byte[512];
            int len = -1;
            is = con.getInputStream();
            bos = new ByteArrayOutputStream();
            while ((len = is.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            return bos;

        }
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage(), ex);
    } finally {
        con.disconnect();
        is.close();
    }
    return bos;
}
 
Example 9
Source File: NetworkUtil.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static HttpURLConnection getHttpConnection(
        String method,
        String targetURL,
        String username,
        String password)
        throws IOException {
    URL url = new URL(targetURL);
    // Open a HTTP connection to the URL
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    String basicAuth = getHTTPBaseAuth(username, password);
    if (null != basicAuth) {
        conn.setRequestProperty("Authorization", basicAuth);
    }
    conn.setRequestProperty("User-Agent", Constants.APP_USER_AGENT);

    // Allow Inputs
    conn.setDoInput(true);
    // Don't use a cached copy.
    conn.setUseCaches(false);
    // Use a post method.
    if(method.length() > 0)
        conn.setRequestMethod(method);

    conn.setConnectTimeout(TIMEOUT_CONNECTION);
    conn.setReadTimeout(TIMEOUT_SOCKET);
    conn.setRequestProperty("Accept", "*/*");

    return isValidUri(targetURL) ? conn : null;
}
 
Example 10
Source File: AbstractRESTClient.java    From hawkular-apm with Apache License 2.0 5 votes vote down vote up
public HttpURLConnection getConnectionForRequest(String tenantId, URL url, String method) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod(method);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setAllowUserInteraction(false);
    addHeaders(connection, tenantId);
    return connection;
}
 
Example 11
Source File: UUIDFetcher.java    From EchoPet with GNU General Public License v3.0 5 votes vote down vote up
private static HttpURLConnection createConnection() throws Exception {
    URL url = new URL(PROFILE_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}
 
Example 12
Source File: Caller.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
private HttpURLConnection openPostConnection(String method, Map<String, String> params)
        throws IOException {
    HttpURLConnection urlConnection = openConnection(apiRootUrl);
    urlConnection.setRequestMethod("POST");
    urlConnection.setDoOutput(true);
    urlConnection.setUseCaches(true);
    OutputStream outputStream = urlConnection.getOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
    String post = buildPostBody(method, params);
    writer.write(post);
    writer.close();
    return urlConnection;
}
 
Example 13
Source File: UserAgentUtils.java    From MyShopPlus with Apache License 2.0 5 votes vote down vote up
/**
 * 通过 IP 获取地址 (淘宝接口)
 *
 * @param ip {@code String} 用户 IP 地址
 * @return {@code String} 用户地址
 */
public static IpInfo getIpInfo(String ip) {
    if ("127.0.0.1".equals(ip)) {
        ip = "127.0.0.1";
    }

    try {
        URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
        HttpURLConnection htpcon = (HttpURLConnection) url.openConnection();
        htpcon.setRequestMethod("GET");
        htpcon.setDoOutput(true);
        htpcon.setDoInput(true);
        htpcon.setUseCaches(false);

        InputStream in = htpcon.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
        StringBuilder temp = new StringBuilder();
        String line = bufferedReader.readLine();
        while (line != null) {
            temp.append(line).append("\r\n");
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
        return MapperUtils.json2pojoByTree(temp.toString(), "data", IpInfo.class);
    } catch (Exception e) {
        return new IpInfo();
    }
}
 
Example 14
Source File: MaximoConnector.java    From maximorestclient with Apache License 2.0 4 votes vote down vote up
protected HttpURLConnection setMethod(HttpURLConnection con, String method, String... properties)
		throws IOException, OslcException {
	this.httpMethod = method;		
	if (this.isGET()) {
		con.setRequestMethod(HTTP_METHOD_GET);
		con.setRequestProperty("accept", "application/json");
		con.setUseCaches(false);
		con.setAllowUserInteraction(false);
		con.setRequestProperty("x-public-uri", this.options.getPublicURI());
	} else if (this.isPOST()) {
		con.setRequestMethod(HTTP_METHOD_POST);
		con.setRequestProperty("Content-Type", "application/json");
		con.setDoOutput(true);
		con.setRequestProperty("x-public-uri", this.options.getPublicURI());
	} else if (this.isPATCH()) {
		con.setRequestMethod(HTTP_METHOD_POST);
		con.setRequestProperty("Content-Type", "application/json");
		con.setDoOutput(true);
		con.setRequestProperty("x-public-uri", this.options.getPublicURI());
		con.setRequestProperty("x-method-override", HTTP_METHOD_PATCH);
	} else if (this.isMERGE()) {
		con.setRequestMethod(HTTP_METHOD_POST);
		con.setRequestProperty("Content-Type", "application/json");
		con.setDoOutput(true);
		con.setRequestProperty("x-public-uri", this.options.getPublicURI());
		con.setRequestProperty("x-method-override", HTTP_METHOD_PATCH);
		con.setRequestProperty("patchtype",HTTP_METHOD_MERGE);
	} else if (this.isBULK()) {
		con.setRequestMethod(HTTP_METHOD_POST);
		con.setRequestProperty("Content-Type", "application/json");
		con.setDoOutput(true);
		con.setRequestProperty("x-public-uri", this.options.getPublicURI());
		con.setRequestProperty("x-method-override", HTTP_METHOD_BULK);
	} else if (this.isSYNC()) {
		con.setRequestMethod(HTTP_METHOD_POST);
		con.setRequestProperty("Content-Type", "application/json");
		con.setDoOutput(true);
		con.setRequestProperty("x-public-uri", this.options.getPublicURI());
		con.setRequestProperty("x-method-override", HTTP_METHOD_SYNC);
	} else if (this.isMERGESYNC()) {
		con.setRequestMethod(HTTP_METHOD_POST);
		con.setRequestProperty("Content-Type", "application/json");
		con.setDoOutput(true);
		con.setRequestProperty("x-public-uri", this.options.getPublicURI());
		con.setRequestProperty("x-method-override", HTTP_METHOD_SYNC);
		con.setRequestProperty("patchtype",HTTP_METHOD_MERGE);
	} else if (this.isDELETE()) {
		con.setRequestMethod(HTTP_METHOD_DELETE);
		con.setRequestProperty("accept", "application/json");
		con.setUseCaches(false);
		con.setAllowUserInteraction(false);
		con.setRequestProperty("x-public-uri", this.options.getPublicURI());
	}
	
	for (String property : properties) {
		con.setRequestProperty("Properties", property);
	}
	
	return con;
}
 
Example 15
Source File: UrlConnectionDownloader.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
@Override public Response load(Uri uri, int networkPolicy) throws IOException {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    installCacheIfNeeded(context);
  }

  HttpURLConnection connection = openConnection(uri);
  connection.setUseCaches(true);

  if (networkPolicy != 0) {
    String headerValue;

    if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
      headerValue = FORCE_CACHE;
    } else {
      StringBuilder builder = CACHE_HEADER_BUILDER.get();
      builder.setLength(0);

      if (!NetworkPolicy.shouldReadFromDiskCache(networkPolicy)) {
        builder.append("no-cache");
      }
      if (!NetworkPolicy.shouldWriteToDiskCache(networkPolicy)) {
        if (builder.length() > 0) {
          builder.append(',');
        }
        builder.append("no-store");
      }

      headerValue = builder.toString();
    }

    connection.setRequestProperty("Cache-Control", headerValue);
  }

  int responseCode = connection.getResponseCode();
  if (responseCode >= 300) {
    connection.disconnect();
    throw new ResponseException(responseCode + " " + connection.getResponseMessage(),
        networkPolicy, responseCode);
  }

  long contentLength = connection.getHeaderFieldInt("Content-Length", -1);
  boolean fromCache = parseResponseSourceHeader(connection.getHeaderField(RESPONSE_SOURCE));

  return new Response(connection.getInputStream(), fromCache, contentLength);
}
 
Example 16
Source File: ClientCamelServletITest.java    From hawkular-apm with Apache License 2.0 4 votes vote down vote up
@Test
public void testInvokeCamelRESTService() throws IOException {
    // Delay to avoid picking up previously reported txns
    Wait.until(() -> getApmMockServer().getTraces().size() == 0);

    URL url = new URL(System.getProperty("test.uri")
            + "/camel-example-servlet-rest-tomcat/rest" + "/user/123");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod("GET");

    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setUseCaches(false);
    connection.setAllowUserInteraction(false);
    connection.setRequestProperty("Content-Type",
            "application/json");

    java.io.InputStream is = connection.getInputStream();

    byte[] b = new byte[is.available()];

    is.read(b);

    is.close();

    assertEquals(200, connection.getResponseCode());

    String user = new String(b);

    assertTrue("Response should contain user with name 'John Doe'", user.contains("John Doe"));

    // Need to wait for trace fragment to be reported to server
    Wait.until(() -> getApmMockServer().getTraces().size() == 1, 2, TimeUnit.SECONDS);

    // Check if trace fragments have been reported
    List<Trace> traces = getApmMockServer().getTraces();

    assertEquals(1, traces.size());
    assertNotNull(traces.get(0).getTraceId());

    // Check top level node is a Consumer associated with the servlet
    assertEquals(Consumer.class, traces.get(0).getNodes().get(0).getClass());
}
 
Example 17
Source File: NetUtils.java    From WayHoo with Apache License 2.0 4 votes vote down vote up
public int uploadFilesByPost(String actionUrl, String fileName, File file) {
	Log.i(TAG, "<uploadFilesByPost> actionUrl:" + actionUrl + " fileName:"
			+ fileName, Log.APP);
	String CHARSET = "UTF-8";

	// 得到响应�?
	int res = 0;
	try {
		URL uri = new URL(actionUrl);
		HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
		conn.setReadTimeout(10 * 1000);
		conn.setDoInput(true);// 允许输入
		conn.setDoOutput(true);// 允许输出
		conn.setUseCaches(false);
		conn.setRequestMethod("POST"); // Post方式
		conn.setRequestProperty("connection", "keep-alive");
		conn.setRequestProperty("Charsert", "UTF-8");

		conn.setRequestProperty("Content-Type", multipart_form_data
				+ ";boundary=" + boundary);

		// 输出�?
		DataOutputStream outStream = new DataOutputStream(
				conn.getOutputStream());

		// 发�?文件数据
		if (file != null) {
			StringBuilder sb1 = new StringBuilder();
			sb1.append(twoHyphens);
			sb1.append(boundary);
			sb1.append(lineEnd);
			// actionData 是自己定义的
			sb1.append("Content-Disposition: form-data; name=\"actionData\"; filename=\""
					+ fileName + "\"" + lineEnd);
			sb1.append("Content-Type: application/octet-stream; charset="
					+ CHARSET + lineEnd);
			sb1.append(lineEnd);
			outStream.write(sb1.toString().getBytes());

			InputStream is = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				outStream.write(buffer, 0, len);
			}

			is.close();
			outStream.write(lineEnd.getBytes());
		}

		// 请求结束标志
		byte[] end_data = (twoHyphens + boundary + twoHyphens + lineEnd)
				.getBytes();

		outStream.write(end_data);
		outStream.flush();
		res = conn.getResponseCode();

		outStream.close();
		conn.disconnect();
	} catch (IOException e) {
		e.printStackTrace();
	}
	return res;

}
 
Example 18
Source File: NetworkRequest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("TryFinallyCanBeTryWithResources")
private void constructMessage(@NonNull HttpURLConnection conn, String token) throws IOException {
  Preconditions.checkNotNull(conn);

  if (!TextUtils.isEmpty(token)) {
    conn.setRequestProperty("Authorization", "Firebase " + token);
  } else {
    Log.w(TAG, "no auth token for request");
  }

  StringBuilder userAgent = new StringBuilder("Android/");
  String gmsCore = getGmsCoreVersion(context);
  if (!TextUtils.isEmpty(gmsCore)) {
    userAgent.append(gmsCore);
  }
  conn.setRequestProperty("X-Firebase-Storage-Version", userAgent.toString());

  Map<String, String> requestProperties = requestHeaders;
  for (Map.Entry<String, String> entry : requestProperties.entrySet()) {
    conn.setRequestProperty(entry.getKey(), entry.getValue());
  }

  JSONObject jsonObject = getOutputJSON();
  byte[] rawOutput;
  int rawSize;

  if (jsonObject != null) {
    rawOutput = jsonObject.toString().getBytes(UTF_8);
    rawSize = rawOutput.length;
  } else {
    rawOutput = getOutputRaw();
    rawSize = getOutputRawSize();
    if (rawSize == 0 && rawOutput != null) {
      rawSize = rawOutput.length;
    }
  }

  if (rawOutput != null && rawOutput.length > 0) {
    if (jsonObject != null) {
      conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
    }
    conn.setDoOutput(true);
    conn.setRequestProperty(CONTENT_LENGTH, Integer.toString(rawSize));
  } else {
    conn.setRequestProperty(CONTENT_LENGTH, "0");
  }

  conn.setUseCaches(false);
  conn.setDoInput(true);

  if (rawOutput != null && rawOutput.length > 0) {
    OutputStream outputStream = conn.getOutputStream();
    if (outputStream != null) {
      BufferedOutputStream bufferedStream = new BufferedOutputStream(outputStream);
      try {
        bufferedStream.write(rawOutput, 0, rawSize);
      } finally {
        bufferedStream.close();
      }
    } else {
      Log.e(TAG, "Unable to write to the http request!");
    }
  }
}
 
Example 19
Source File: NetUtils.java    From WayHoo with Apache License 2.0 4 votes vote down vote up
/**
 * 使用post的方式,提交表单,不包括文件上传(新服务器)
 * 
 * @param actionUrl
 * @param params
 * @param files
 * @return
 * @throws IOException
 */
public static boolean uploadParamsByPost(String actionUrl,
		Map<String, String> params) throws IOException {
	Log.i(TAG, "<uploadParamsByPost> actionUrl:" + actionUrl + " params:"
			+ params, Log.APP);
	String BOUNDARY = java.util.UUID.randomUUID().toString();
	String PREFIX = "--", LINEND = "\r\n";
	String MULTIPART_FROM_DATA = "multipart/form-data";
	String CHARSET = "UTF-8";

	URL uri = new URL(actionUrl);
	HttpURLConnection conn = (HttpURLConnection) uri.openConnection();

	conn.setReadTimeout(10 * 1000);
	conn.setConnectTimeout(10 * 1000);
	conn.setDoInput(true);// 允许输入
	conn.setDoOutput(true);// 允许输出
	conn.setUseCaches(false);
	conn.setRequestMethod("POST"); // Post方式
	conn.setRequestProperty("connection", "keep-alive");
	conn.setRequestProperty("Charsert", "UTF-8");

	conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
			+ ";boundary=" + BOUNDARY);

	// 首先组拼文本类型的参数
	StringBuilder sb = new StringBuilder();
	for (Map.Entry<String, String> entry : params.entrySet()) {
		sb.append(PREFIX);
		sb.append(BOUNDARY);
		sb.append(LINEND);
		sb.append("Content-Disposition: form-data; name=\""
				+ entry.getKey() + "\"" + LINEND);
		sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
		sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
		sb.append(LINEND);
		sb.append(entry.getValue());
		sb.append(LINEND);
	}

	DataOutputStream outStream = new DataOutputStream(
			conn.getOutputStream());
	outStream.write(sb.toString().getBytes());

	// 请求结束标志
	byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
	outStream.write(end_data);
	outStream.flush();

	outStream.close();
	// 得到响应号
	int res = conn.getResponseCode();
	if (res == 200) {
		return true;
	}
	conn.disconnect();

	return false;
}
 
Example 20
Source File: MediaWikiClient.java    From git-changelog-lib with Apache License 2.0 4 votes vote down vote up
private String postToWiki(HttpState httpState, String addr, String postContent)
    throws GitChangelogIntegrationException {
  try {
    logger.info("Posting to: " + addr);
    final HttpURLConnection conn = openConnection(new URL(addr));
    try {
      conn.setRequestMethod("POST");
      if (httpState.getCookieString().isPresent()) {
        logger.info("Using cookie: " + httpState.getCookieString().get());
        conn.setRequestProperty("Cookie", httpState.getCookieString().get());
      }

      if (postContent != null) {
        final String postContentObf =
            postContent.replaceAll("lgpassword=([^&]+)", "lgpassword=*");
        logger.info("Post content: " + postContentObf);
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("charset", "utf-8");
        conn.setRequestProperty("Content-Length", postContent.length() + "");
        conn.setUseCaches(false);
        try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
          wr.writeUTF(postContent);
        }
      }

      if (conn.getHeaderFields().get("Set-Cookie") != null
          && conn.getHeaderFields().get("Set-Cookie").size() > 0
          && !httpState.getCookieString().isPresent()) {
        httpState.setCookieString(conn.getHeaderFields().get("Set-Cookie").get(0));
        logger.info("Got cookie: " + httpState.getCookieString().orNull());
      }

      final String response = getResponse(conn);
      logger.info("Response: " + response);
      return response;
    } finally {
      conn.disconnect();
    }
  } catch (final Exception e) {
    throw new GitChangelogIntegrationException(addr, e);
  }
}