我[目前正在使用这样的东西
HttpURLConnection con = (HttpURLConnection) u.openConnection ();
con.setDoInput(true);
con.setRequestMethod("POST");
con.setDoInput (true);
con.setDoOutput (true);
con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
out = new DataOutputStream(con.getOutputStream());
String content = "username=" + URLEncoder.encode ("bob")
+ "&password=" + URLEncoder.encode ("smith");
System.out.println("\n" + "sending form to HTTP server ...");
out.writeBytes (content);
out.flush ();
out.close ();
con.connect();
Run Code Online (Sandbox Code Playgroud)
有了这个,我设法将一些数据传递给我的服务器.我现在正在徘徊的是这样可以发送多少钱?
我希望能够发送一些xml文件(100~200行),想知道我能不能这样做?
贾森
我正在开发Android设备的应用程序(2.2).我正在尝试获取一个html页面.我使用的java代码似乎工作,所以我可以获得我想要的页面,但有时应用程序崩溃,我得到例外:"没有对等证书".发生这种情况时,如果我等待一个小时或者我改变网络,它会正常工作.有人知道如何解决这个问题吗?该网站是https://stud.infostud.uniroma1.it/Sest/Log/
我真的很抱歉我的英语,我希望你能理解!
虽然我使用GZIPInputStream来压缩来自Internet的字节,但程序运行错误如下:
05-08 17:37:02.465: W/System.err(744): java.io.IOException: unknown format (magic number 213c)
05-08 17:37:02.465: W/System.err(744): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:84)
05-08 17:37:02.465: W/System.err(744): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:64)
05-08 17:37:02.475: W/System.err(744): at com.Android.Sample.TestActivity.onCreate(TestActivity.java:54)
05-08 17:37:02.475: W/System.err(744): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-08 17:37:02.475: W/System.err(744): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 17:37:02.475: W/System.err(744): at android.os.Looper.loop(Looper.java:123)
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-08 17:37:02.475: W/System.err(744): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 17:37:02.475: W/System.err(744): at java.lang.reflect.Method.invoke(Method.java:507)
05-08 17:37:02.475: W/System.err(744): at …Run Code Online (Sandbox Code Playgroud) 我一直试图解决这个问题几个小时了.我已经找到了答案,但我找不到答案.我以前从未打扰过这个问题,这是我第一次提出问题.基本上我正在做的是为了组织目的打破我的编码.下面的代码片段工作得很好,但是当我把它拿到另一个类时,urlConnect(); 连接就好了.我在下面标记了它.
public String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
String line = "";
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
/* Connecting to url */
urlConnection.connect(); <-------------------------works in this snippet
/* Reading data from url */
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
while((line = br.readLine()) != null){
sb.append(line);
} …Run Code Online (Sandbox Code Playgroud) 我想通过POST方法将xml字符串传递给URL.
我试过下面的代码片段,但它没有返回任何内容
disableCertificateValidation();
String url = "https://..url"; //https
Properties sysProps = System.getProperties();
sysProps.put("proxySet", "true");
sysProps.put("proxyHost", "1.2.3.4");
sysProps.put("proxyPort", "80");
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("userid",
"password".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
String xml = ---xml string;
URL urll;
HttpURLConnection connection = null;
try {
// Create connection
urll = new URL(url);
connection = (HttpURLConnection) urll.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", ""
+ Integer.toString(xml.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream wr = new DataOutputStream(connection
.getOutputStream()); …Run Code Online (Sandbox Code Playgroud) 我尝试编写一个简单的脚本,每小时检查一次网站,并在发现有空时向我发送电子邮件。
我认为每隔一小时执行此操作不会引发任何问题,但出现以下错误:
MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=60745): Max retries exceeded with url: /session/900f45d6c8c800f2a8ebcf43daa05b69/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fa42c261c10>: Failed to establish a new connection: [Errno 61] Connection refused'))
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import schedule
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from notification import *
#script i have to send an email (works fine)
PATH = "mypath"
# i have the path where there drivers are
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
# to not open the browser
driver = webdriver.Chrome(options=chrome_options, executable_path=PATH) …Run Code Online (Sandbox Code Playgroud) python selenium google-chrome httpconnection selenium-chromedriver
如果MaxIdleConnsPerHost设置为较高的数字,比如说 1000,打开的连接数仍将取决于其他主机,对吗?我的意思是,只要其他主机没有关闭这些连接,允许与同一主机的 1000 个空闲连接将导致打开 1000 个连接?
那么,有效地将此值设置为一个较高的数字,会导致永远不会关闭连接,而是等待其他主机关闭吗?我是否正确解释了这一点?
我正在尝试使用HTTPConnection(2.7.8)发出请求,我已将超时设置为10 HTTPConnection(host, timeout=10).但是,HTTPConnection.request()10秒后似乎没有超时.事实上,HTTPConnection.timeout似乎甚至没有被阅读HTTPConnection.request()(它只是通过阅读HTTPConnection.connect().我的理解是正确的吗?timeout只适用于connect()而不是request()吗?有没有办法超时request()?
更新:
我想我已经进一步缩小了这个问题:如果我不提供该方案,它将不会尊重套接字超时.如果提供了该方案,即完整的URL http://google.com:22222,则它会相应地超时.我想知道为什么该计划的存在应该有所作为.也就是说,以下不尊重超时
socket.setdefaulttimeout(3)
conn = HTTPConnection('google.com:22222')
conn.timeout = 3
conn.request('GET', '')
Run Code Online (Sandbox Code Playgroud)
然而,这样做:
socket.setdefaulttimeout(3)
conn = HTTPConnection('http://google.com:22222')
conn.timeout = 3
conn.request('GET', '')
Run Code Online (Sandbox Code Playgroud)
但是,它不会发生在所有域中.
谢谢
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(
"product_name", product_name));
nameValuePairs.add(new BasicNameValuePair("brand",
brand_product));
nameValuePairs.add(new BasicNameValuePair("reference_price",
mrp_product));
nameValuePairs.add(new BasicNameValuePair("model",
model_product));
HttpPost httppost = new HttpPost(
"http://10.0.2.2/wic3/wic2/product/doadd");
httppost.setEntity(new UrlEncodedFormEntity(
nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = SignUpActivity.httpclient.execute(
httppost, responseHandler);
Log.d("response", response);
Intent intent = new Intent(ShareProductActivity.this, ShareActivity.class);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
这给了我一个例外:
05-07 14:56:19.105: D/exception(1137): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
我搜索了这个,每个人都说改变127.0.0.1了10.0.2.2,但我没有使用127.0.0.1
我想问题就在这一步:
String response = SignUpActivity.httpclient.execute(httppost, responseHandler);
Run Code Online (Sandbox Code Playgroud) 我正在尝试检测是否确实存在互联网连接并且网站可以访问.我有一个广播接收器,在接收时运行以下方法:
public boolean hasInternetNow() {
Thread checkinternet = new Thread(new Runnable() {
@Override
public void run() {
try {
try {
ConnectivityManager cm = (ConnectivityManager) myContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url .openConnection();
urlc.setRequestProperty("User-Agent", "test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000); // mTimeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
setinternetDetected(true);
} else {
setinternetDetected(false);
}
}
} catch (IOException e) {
e.printStackTrace();
setinternetDetected(false);
} catch (NullPointerException e){
e.printStackTrace();
setinternetDetected(false);
}
} catch (Exception …Run Code Online (Sandbox Code Playgroud) 我在Spring WS 2.1.0上有一个Web服务.Web服务在Tomcat 7上运行,并使用一种事务方法实现端点,该方法从数据库读取数据并生成不同的报告.Tomcat 7通过JServ协议在Apache Server后面运行.
在通过Apache JMeter进行压力测试期间,我得出结论,同时处理请求是串行处理的.起初我试图调整数据库连接池(commons-dbcp和更高版本的tomcat-jdbc),但结果是一样的.端点方法只读取数据,因此没有读后读或写后读依赖,并且可以并行处理事务.因此问题在于HTTP连接处理.
经过一些搜索(当然还有谷歌搜索),我发现Spring WS附带了单线程的默认连接管理器(SimpleHttpConnectionManager).好的做法是用MultiThreadedHttpConnectionManager替换SimpleHttpConnectionManager.我找到了如下所示的示例代码:
<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
<constructor-arg>
<bean class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager>
<property name="maxConnectionsPerHost" value="20"/>
<property name="maxTotalConnections" value="100"/>
</bean>
</constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
但是这个例子对我来说并不是很清楚,因为我不明白Spring如何使用HttpClient(指代ID或自动装配它).那我怎么能在我的案例中使用MultiThreadedHttpConnectionManager和Spring?我应该在哪里注入此类的已配置实例?
假设我没有Grid设置,我可以从Selenium WebDriver启动的最大并发Chrome线程数是多少?
是5吗?而且对Chrome Headless也适用吗?
connection selenium http persistent-connection httpconnection
我得到了一些我似乎无法解决的错误......这是示例代码,所以我很困惑发生了什么.错误被注释到它们出现的行的一侧.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class Connect {
URL url;
URLConnection urlConnection;
DataOutputStream outStream;
DataInputStream inStream;
// Build request body
String body = "fname=" + URLEncoder.encode("Atli", "UTF-8"); //Syntax error on token ";", { expected after this token
// Create connection
url = new URL("http://192.168.1.68/test/POST/post.php");
urlConnection = url.openConnection();
((HttpURLConnection)urlConnection).setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", ""+ body.length());
// Create I/O streams
outStream = new DataOutputStream(urlConnection.getOutputStream());
inStream = new DataInputStream(urlConnection.getInputStream());
// Send request
outStream.writeBytes(body);
outStream.flush(); …Run Code Online (Sandbox Code Playgroud) httpconnection ×13
java ×6
android ×5
python ×2
selenium ×2
connect ×1
connection ×1
connectivity ×1
go ×1
gzip ×1
http ×1
httpclient ×1
httplib ×1
https ×1
post ×1
servlets ×1
spring ×1
ssl ×1
tomcat7 ×1
unknown-host ×1
xml ×1