スキップしてメイン コンテンツに移動

投稿

ラベル(Http)が付いた投稿を表示しています

Java: How to Set Proxy on HttpURLConnection

If you need to set proxy address on Java HttpURLConnection. Do something like below. public static String getString(String urlStr) throws Exception { HttpURLConnection connection = null; InputStream is = null; try { URI uri = new URI(urlStr); // settin proxy String proxyHost = ""; // your proxy serever address int proxyPort = 8080; // your proxy port Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); // open connection with passing Proxy Object connection = (HttpURLConnection)uri.toURL().openConnection(proxy); connection.connect(); is = connection.getInputStream(); String content = toString(is); return content; } finally{ closeQuietly(is); if(connection != null) { connection.disconnect(); } } } private static String toString(InputStream is) throws IOException { byt

Check window.opener permission

Issue I tried to access some properties (or call methods) defined in http parent window via window.opener in pop-upped https window, which is created from the parent window. But it simply fails when accessing window.opener.a_property in the child window because the protocol is different between the child and parent window. e.g. http and https Remember this browser behavior is absolutely correct. The point of this post is how to avoid simply stopping javascript when accessing properties in window.opener. Browsers I confirmed this permission issue happens FireFox 17.0.1 and Internet Explore 9. Somehow Chrome doesn't complain it at all. the code simply passed. Solution In order to avoid this, I wrote the following conditions to detect the window.opener is accessible or not before accessing window.opener, but all of them don't return expected result at all. if(window.opener) // return true :( if(window.opener == undefined) // return true :( Finally I have reached at

WinHttp.WinHttpRequestを使ってHttp PostでByte配列やString配列を送信するプログラム(Excel VBA)

WinHttp.WinHttpRequestオブジェクトを使って使ってHttp PostでByte配列のデータを送信するExcel VBAプログラムです。 WinHttp.WinHttpRequestを使う際には、 1. ExcelのMicrosoft Visual Basic エディタのメニューバーから「ツール->参照設定」とたどる。 2. 表示されたダイアログからMicrosoft WinHTTP Serviceにチェックを入れる。 という手順が必要です。 Byte配列をPOST Private Const BOUNDARY As String = "SOMETHING" Private Function httpPostServletByte(url As String, data() As Byte) As Byte() On Error GoTo e Dim WinHttpReq As WinHttp.WinHttpRequest If (WinHttpReq Is Nothing) Then Set WinHttpReq = New WinHttpRequest End If WinHttpReq.Open "POST", url, False WinHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded; boundary=" & BOUNDARY WinHttpReq.send data Dim response() As Byte: response = WinHttpReq.ResponseBody httpPostServletByte = response Set WinHttpReq = Nothing Exit Function e: Set WinHttpReq = Nothing Debug.Print "httpPost Error:" & Err.

WebRequestクラスを使ってStringをPostするコード

使用するEncodingはUTF-8を仮定しています。 public static void PostData(string host, int port, string data) { try { Uri requestUri = new Uri(string.Format("http://{0}:{1}/", host, port)); WebRequest request = WebRequest.Create(requestUri); request.Proxy = new WebProxy(); request.Method = "POST"; byte[] postDataBytes = Encoding.UTF8.GetBytes(data); request.ContentLength = postDataBytes.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(postDataBytes, 0, postDataBytes.Length); WebResponse response = request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { string strResp = reader.ReadToEnd(); // Do something... } } } catch (Exception ex) { log.ErrorFormat("Error in {0} method\n{1}", MethodBase.GetCurrentM