我有以下python脚本,我想发送"假"标题信息,以便我的应用程序就好像它是firefox.我怎么能这样做?
import urllib, urllib2, cookielib
username = '****'
password = '****'
login_user = urllib.urlencode({'password' : password, 'username' : username})
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
response = opener.open("http://www.***.com")
response = opener.open("http://www.***.com/login.php")
response = opener.open("http://www.***.com/welcome.php", login_user)
Run Code Online (Sandbox Code Playgroud) 我header('HTTP/1.0 404 Not Found');在代码中有一个地方但由于某种原因它没有重定向到Apache的默认404页面.
我在.htaccess文件上有一个重写规则,它将每个请求重定向到index.php.这可能是问题吗?
我正在开发一个PHP应用程序,它使用HTTP响应代码进行通信以及响应主体.所以这是PHP代码中的典型场景:
try {
doSomething();
}
catch (Exception $e) {
header('HTTP/1.1 500 Internal Server Error');
}
Run Code Online (Sandbox Code Playgroud)
...而客户端中的典型代码如下所示:
switch(responseCode) {
case 200 :
// all fine
// ....
case 500 :
// trouble!
}
Run Code Online (Sandbox Code Playgroud)
这很酷,只要PHP代码中捕获到每个错误.
问题:如果由于PHP代码中的任何原因发生未被捕获的错误或语法错误等无法捕获的错误,Apache将发送200 OK.但我不想说500内部服务器错误.也许每个.htaccess左右.
我希望HTTP响应从现在起24小时后过期(这意味着浏览器直到明天才会对该URL发出任何请求).但是,如果请求在到期后明天重新发出,我想确保浏览器将发送正确的请求标头,以便服务器发送304而不是强制客户端重新下载整个响应主体,如果它没有在服务器上没有改变.而且我希望304也会在24小时后过期.
首先,这个场景可能吗?或者我必须在Expiration风格的缓存和304风格的缓存之间做出选择,但不是两者都有?如果可能的话,那么正确的响应头(初始响应和后续的304)是什么?
如果经常发生,答案会根据浏览器类型/版本而有所不同,那么哪些标题适用于哪些浏览器 - 以及哪些浏览器无法完成我想要的操作?我只对目前使用的最常见的浏览器感兴趣(例如IE6 +,FF3 +,Chrome最新版,Safari最新版)?
如果已经在SO上询问了这个答案,我会道歉 - 我搜索了一会儿,然后空白了.
澄清:我问的是这个问题,因为我正在组建一个自动测试套件来验证,无论服务器平台如何,Web应用程序正在生成正确的HTTP标头,以生成客户端缓存行为,我们希望我们的所有Web应用程序都可以有.所以我对如何配置Apache/IIS/PHP/Rails/Django/JSP/ASP.NET /等不感兴趣(至少现在).生成正确的标题.我只想知道,在HTTP层,只有正确的标题是什么.
更新:我发现这个问题回答了我的部分问题.根据RFC 2616 10.3.5,它说,我应该在服务器返回的304s中包含一个Expires:或Cache-Control: max-age标题.这绝对是理想的行为.
然而,这个问题没有回答的是这种符合RFC的方法是否适用于现有的流行浏览器,尤其是IE6/7/8,它们是通常的标准兼容罪魁祸首,还有IE9,FF4 +,最新Chrome和最新版本我们的应用程序也必须支持的Safari.如果这些浏览器中的任何一个不符合RFC的要求,我可以使用变通方法吗?
我正在尝试添加自定义安全功能,其中客户端为Silverlight客户端发出的每个服务调用添加一个令牌,然后该服务可以访问此令牌来控制应用程序安全系统.我试图通过实现IClientMessageInspector接口并将其链接到我生成的服务客户端来实现此目的.我没有使用Visual Studio生成的代理,而是使用ChannelFactory创建的自己的客户端.我已经设法在网上找到了几个似乎都使用两种基本方法之一的解决方案.首先,通过向呈现给IClientMessageInspector.BeforeSendRequest的Message的headers集合添加标头,其次通过使用HttpRequestMessageProperty将信息作为属性添加到呈现给IClientMessageInspector.BeforeSendRequest的Message.我一直在尝试这两种方法而没有任何成功.看来两种技术都成功地将数据添加到请求中,但我无法在服务器上访问.我想补充一点,对我来说这是一个非常新的领域,由于缺乏经验,我很可能在互联网上错过了答案.
生成我的客户端的代码是:
private ISecurityAdministrationContract CreateChannel()
{
if (factory == null)
{
lock (this)
{
// Create a custom binding that uses HTTP and binary encoding.
var elements = new List<BindingElement>();
elements.Add(new BinaryMessageEncodingBindingElement());
elements.Add(new HttpTransportBindingElement());
var binding = new CustomBinding(elements);
// Create a channel factory for the service endpoint configured with the custom binding.
factory = new ChannelFactory<ISecurityAdministrationContract>(binding, new EndpointAddress(SecurityAdminServiceAddress));
//Add my IClientMessageInspector
factory.Endpoint.Behaviors.Add(new ClientSecurityInterceptor());
}
}
ISecurityAdministrationContract client = factory.CreateChannel();
return client;
}
}
ISecurityAdministrationContract client = factory.CreateChannel();
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试从以下位置获取PDF文件:
网址:https:// domain_name/xyz/_id/download /
其中它不指向直接pdf文件,并且下载每个唯一文件来解释特定的<_id>字段.
我把这个链接放在浏览器的地址栏中,然后立即下载Pdf文件,而当我尝试通过HTTPsURLConnection获取它时,它的Content-Type是'text/html'形式,而它应该是'application/pdf' .
在连接之前我还尝试'setRequestProperty'为'application/pdf',但文件总是以'text/html'形式下载.
我用它的方法是'GET'
1)我是否需要使用HttpClient而不是HttpsURLConnection?
2)这些类型的链接是否用于增加安全性?
3)请指出我的错误.
4)我怎么知道服务器上的文件名?
我粘贴在我实施的主要代码之下:
URL url = new URL(sb.toString());
//created new connection
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
//have set the request method and property
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/pdf");
Log.e("Content Type--->", urlConnection.getContentType()+" "+ urlConnection.getResponseCode()+" "+ urlConnection.getResponseMessage()+" "+urlConnection.getHeaderField("Content-Type"));
//and connecting!
urlConnection.connect();
//setting the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File SDCardRoot = Environment.getExternalStorageDirectory();
//created …Run Code Online (Sandbox Code Playgroud) 如果我调用header('Content-Type:text/plain; charset=ISO-8859-15');浏览器将下载文件而不是显示它.text/html改为使用作品.(无论如何处理下载的文件,它不下载源代码)
我试图添加,header('Content-Disposition:inline;');但它只是被忽略了.
我对于什么可能导致这个问题,任何小费都很无能为力?
服务器是MAMP 1.9.6(PHP 5.3.5,Apache/2.0.64).
编辑:这只发生在Chrome上,适用于Firefox,Camino和Safari.
我猜PHP的get_headers不允许上下文,因此我必须更改默认流上下文以仅获取请求的HEAD.这会导致页面上的其他请求出现一些问题.我似乎无法弄清楚如何重置默认流上下文.我正在尝试这样的事情:
$default = stream_context_get_default(); //Get default stream context so we can reset it
stream_context_set_default( //Only fetch the HEAD
array(
'http' => array(
'method' => 'HEAD'
)
)
);
$headers = get_headers($url, 1); //Url can be whatever you want it to be
//var_dump($headers);
var_dump($default);
stream_context_set_default($default); //This doesn't work as it expects an array and not a resource pointer
Run Code Online (Sandbox Code Playgroud)
有谁知道解决这个问题?
我知道有人建议使用Curl,但我不愿意这个.谢谢!
private void setUpRestClient() {
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Accept", "application/pyur.v1")
.header("Authorization", new SharedPreferencesUtil(getBaseContext()).getToken())
.header("Content-Type", "application/json")
.method(original.method(),original.body())
.build();
return chain.proceed(request);
}
});
RestClient.getInstance().configureRestAdapter(this, getResources().getString(R.string.base_url),client);
}
public void configureRestAdapter(final Context context, String baseUrl, OkHttpClient client) {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
service = retrofit.create(NetworkServiceInterface.class);
}
Run Code Online (Sandbox Code Playgroud)
这现在让我在Retrofit 2.0中失败了,最初我没有"授权"标题,并且它给了我未经授权的,这是可以理解的.但是现在我用我的身份验证令授权它并且它失败了.Retrofit …
http-headers ×10
php ×4
apache ×3
http ×2
android ×1
caching ×1
content-type ×1
head ×1
header ×1
httprequest ×1
interceptor ×1
java ×1
mime-types ×1
mod-rewrite ×1
post ×1
python ×1
retrofit2 ×1
stream ×1
vary ×1
wcf ×1