Tag: apache commons httpclient

Java – 将OutputStream上传为HTTP文件上载

我有一个写入OutputStream的遗留应用程序,我希望将此流的内容作为文件上载到Servlet。 我已经使用JMeter测试了使用commons-fileupload的Servlet,它运行得很好。 我会使用Apache HttpClient,但它需要一个File而不仅仅是一个输出流。 我不能在本地写一个文件; 如果有一些内存中的File实现可能可行吗? 我尝试过使用HttpURLConnection (下面),但服务器响应“MalformedStreamException:Stream意外结束”。 URL url = new URL(“http”, “localhost”, 8080, “/upload”); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); String boundary = “—————————7d226f700d0”; connection.setRequestProperty(“Content-Disposition”, “form-data; name=\”file\””); connection.setRequestProperty(“Content-Type”, “multipart/form-data; boundary=”+boundary); connection.setRequestProperty(“Accept”, “application/json”); connection.setRequestMethod(“POST”); connection.setChunkedStreamingMode(0); connection.connect(); OutputStream out = connection.getOutputStream(); byte[] boundaryBytes =(“–” + boundary + “\r\n”).getBytes(); out.write(boundaryBytes); //App writes to outputstream here out.write(“\r\n”.getBytes()); […]

从Commons HttpClient迁移到HttpComponents Client

我想从Commons HttpClient(3.x)迁移到HttpComponents Client(4.x),但是如何处理重定向很困难。 代码在Commons HttpClient下正常工作,但在迁移到HttpComponents Client时中断。 一些链接会产生不良重定向,但当我将“http.protocol.handle-redirects”设置为“false”时,大量链接会完全停止工作。 Commons HttpClient 3.x: private static HttpClient httpClient = null; private static MultiThreadedHttpConnectionManager connectionManager = null; private static final long MAX_CONNECTION_IDLE_TIME = 60000; // milliseconds static { //HttpURLConnection.setFollowRedirects(true); CookieManager manager = new CookieManager(); manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(manager); connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(1000); // will need to set from properties file connectionManager.getParams().setMaxTotalConnections(1000); […]

使用非常规方案获取重定向请求的最终URL

我的代码(下面)尝试从服务器返回的最终URL进行一些重定向。 只要URL具有http方案,它就可以正常工作。 当我想要返回具有不同方案的URL时,我的问题出现了。 最终,我希望在某些情况下返回market:// url或其他应用程序启动方案,因为这是针对Android的,我想要与它们一起启动Intent。 所以这让我得到了最终的http url,但是当最终的url是market://它会抛出看到的exception(java.lang.IllegalStateException:Scheme’market’未注册),然后getURI不提供那个,它会提供之前的任何东西。 DefaultHttpClient client = new DefaultHttpClient(); HttpContext httpContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet(mInitialUrl); try { client.execute(httpGet, httpContext); } catch (IllegalStateException e) { e.printStackTrace(); } // Parse out the final uri. HttpHost currentHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); HttpUriRequest req = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST); return (req.getURI().isAbsolute()) ? req.getURI().toString() : (currentHost.toURI() […]

使用Java将音频从44.1kHz下采样到16kHz

我有一个应用程序,它记录来自用户麦克风的语音样本,并将其上传到服务器,然后服务器用它做一些事情。 看来我必须使用以下参数进行记录以避免IllegalArgumentException : Encoding encoding = AudioFormat.Encoding.PCM_SIGNED; float sampleRate = 44100.0F; int sampleSizeInBits = 16; int channels = 2; int frameSize = 4; float frameRate = 44100.0F; boolean bigEndian = false; 但是我需要将它记录在16khz,而不是44.1,(sampleRate和framerate都是,我假设)并且它必须是单声道(1声道)。 PCM签名也是强制性的,所以这很好。 (服务器非常挑剔,我无法对其进行任何更改。)如何使用Java进行转换? 我通过HttpClient将音频文件作为Filebody提交给servlet,将其保存在服务器上,然后进行处理。

HttpClient 4.2,基本身份validation和AuthScope

我有一个应用程序连接到需要基本身份validation的站点。 这些站点在运行时提供,在编译时不知道。 我正在使用HttpClient 4.2。 我不确定下面的代码是否应该如何指定基本身份validation,但文档会建议它。 但是,我不知道在AuthScope的构造函数中传递什么。 我原以为null参数意味着所提供的凭据应该用于所有URL,但它会抛出NullPointerException ,所以显然我错了。 m_client = new DefaultHttpClient(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(m_userName, m_password); ((DefaultHttpClient)m_client).getCredentialsProvider().setCredentials(new AuthScope((HttpHost)null), credentials);

从HttpClient 3.x迁移到4.x.

我们使用commons-httpclient-3.1编写了一些需要转换为4.1的代码。 我对此有点熟悉,现在正在阅读4.1 httpclient教程 。 我在这里看到了几个post(例如, 从HttpClient 3转换为4,但这是关于特定的构造)。 似乎应该有一些文档/示例显示如何将3.x的使用升级到4.x? 一个具体的例子:替换使用org.apache.commons.httpclient.HttpState

为什么这个简单的SOAP客户端不工作(org.apache.http)?

我想将一个XML文件作为请求发送到SOAP服务器。 这是我的代码:( 使用org.apache.http从使用SOAP操作发送HTTP Post请求进行了修改) import org.apache.http.client.*; import org.apache.http.client.methods.*; import org.apache.http.impl.client.*; import org.apache.http.entity.StringEntity; import org.apache.http.protocol.HTTP; import org.apache.http.HttpResponse; import java.net.URI; public static void req() { try { HttpClient httpclient = new DefaultHttpClient(); String body=”xml here”; String bodyLength=new Integer(body.length()).toString(); URI uri=new URI(“http://1.1.1.1:100/Service”); HttpPost httpPost = new HttpPost(uri); httpPost.setHeader( “SOAPAction”, “MonitoringService” ); httpPost.setHeader(“Content-Type”, “text/xml;charset=UTF-8”); StringEntity entity = new StringEntity(body, […]

从HttpClient 3转换为4

我设法对以下所有内容进行了更改: HttpClient client; HttpPost method; client = new DefaultHttpClient(); method = new HttpPost(url); InputStream rstream; try { rstream = method.getResponseBodyAsStream(); } catch (IOException e) { return BadSpot(e.getMessage()); } 我不确定的是我应该替换getResponseBodyAsStream()。

使用有效的客户端证书时HttpClient 403错误

我正在尝试使用Java在网站上自动执行某些任务。 我有一个有效的网站客户端(当我使用firefox登录时工作),但当我尝试使用http客户端登录时,我一直收到403错误。 请注意,我希望我的信任商店信任任何东西(我知道它不安全,但此时我并不担心)。 这是我的代码: KeyStore keystore = getKeyStore();//Implemented somewhere else and working ok String password = “changeme”; SSLContext context = SSLContext.getInstance(“SSL”); KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmfactory.init(keystore, password.toCharArray()); context.init(kmfactory.getKeyManagers(), new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException { […]

HttpClient支持多种TLS协议

我们正在编写一个必须使用HTTPS与几台服务器通信的应用程序。 它需要与AWS(使用AWS库)以及使用TLS 1.2的一些内部服务进行通信。 我开始通过更改我的HttpClient来使用TLS 1.2 SSLContext: public static SchemeRegistry buildSchemeRegistry() throws Exception { final SSLContext sslContext = SSLContext.getInstance(“TLSv1.2”); sslContext.init(createKeyManager(), createTrustManager(), new SecureRandom()); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme(“https”, 443, new SSLSocketFactory(sslContext))); return schemeRegistry; } 并将此SchemeRegistry注入到DefaultHttpClient对象中(通过spring),但这样做我从AWS得到错误,因此我假设(我可能错了)AWS不支持TLS 1.2(如果我只是,我不会收到此消息使用正常的DefaultHttpClient): AmazonServiceException: Status Code: 403, AWS Service: AmazonSimpleDB, AWS Request ID: 5d91d65f-7158-91b6-431d-56e1c76a844c, AWS Error Code: InvalidClientTokenId, AWS Error […]