例如,我有一个由另一个aspx调用的ASP.NET表单:
string url = "http://somewhere.com?P1=" + Request["param"];
Response.Write(url);
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
string url = "http://somewhere.com?P1=" + Request["param"];
string str = GetResponse(url);
if (str...) {}
Run Code Online (Sandbox Code Playgroud)
我需要得到任何Response.Write得到的结果或者去url,操纵那个响应,然后发回别的东西.
任何帮助或正确方向的一点将不胜感激.
我想添加一些纯文本和两个标题HttpServletResponse,代码如下:
resp.setContentType("text/plain");
resp.getWriter().write(messages.get(next).getContent());
resp.addHeader("success", "yes");
resp.addHeader("hasnext", ((Boolean)hasNext).toString());
我遇到的问题是发送内容会阻止发送标头.如果我不写内容,标题会被正确收到,如果我包含他们没有的文本.
问题是什么 ?
在视图中,我使用以下命令创建新文件:
sys.stdout = open(backup_name, 'w')
call_command('dumpdata')
Run Code Online (Sandbox Code Playgroud)
现在如何将该文件返回给用户?
我试图将HttpResponse中的mimetype更改为“ application / json”,但如何添加文件内容以进行响应?
或者,也许还有其他方法可以返回文件?
。
我的JSON看起来像这样 -
{"ipinfo": {
"ip_address":"4.2.2.2",
"ip_type":"Mapped",
"Location":{
"continent":"north america",
"latitude":33.499,
"longitude":-117.662,
"CountryData":{
"country":"united states",
"country_code":"us"},
"region":"southwest",
"StateData":{
"state":"california",
"state_code":"ca"},
"CityData":{
"city":"san juan capistrano",
"postal_code":"92675",
"time_zone":-8}}
}}
Run Code Online (Sandbox Code Playgroud)
这是我的下面代码,它试图访问JSONArray中的项目成员
try {
String url = service + version + method + ipAddress + format;
StringBuilder builder = new StringBuilder();
httpclient = new DefaultHttpClient();
httpget = new HttpGet(url);
httpget.getRequestLine();
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = entity.getContent();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
for (String line = …Run Code Online (Sandbox Code Playgroud) 我有简单的硒类.它运作得很好.现在我很感兴趣,如何在控制台模式下唱歌.换句话说.我需要一个结果(在代码中,如果请求成功与否.)我不需要在Web浏览器中显示.如果一切顺利,我需要一个返回值,如果不是另一个返回值(如真或假);
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
WebDriver driver;
WebElement loginInput;
WebElement passwordInput;
WebElement loginSubmit;
driver = new FirefoxDriver();
driver.get("URL");
loginInput = driver.findElement(By.id("id"));
loginInput.sendKeys("ninotyesh");
passwordInput =driver.findElement(By.id("id"));
passwordInput.sendKeys("key");
loginSubmit = driver.findElement(By.id("id"));
loginSubmit.click();
Run Code Online (Sandbox Code Playgroud) 我试图从http响应中获取图像,但无法将流转换为位图.请让我知道,我在这里失踪了什么.
仅供参考 - 图像内容作为原始二进制文件及其jpeg图像接收.
程序如下:
在postExecute的AsyncTask中执行此操作
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(endpoint);
// Adding Headers ..
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == 200) {
// Get hold of the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
return instream;
// instream.close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在postExecute的AsyncTask中执行此操作
if (null != instream) {
Bitmap bm = BitmapFactory.decodeStream(instream);
if(null …Run Code Online (Sandbox Code Playgroud) 我很少尝试一些HttpServlet东西来更好地理解它。我想构建一个请求传入的场景,我需要相应且尽可能快地发送响应,并在以后在Servlet中做更多工作。根据我目前的理解,仅应在返回doGet或doPost方法时将响应发送到客户端。但是从我的示例来看,响应已在Servlet中的命令处理过程中发送回客户端。因此,当我不希望它返回时,它已经返回了。
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(DisplayHeader.class.getName()).log(Level.SEVERE, null, ex);
}
response.setContentType("text/plain; charset=ISO-8859-1");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
final StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
//TODO most be implemented SynchronizedStatusCodeDimo
out.println("StatusCode=0");
out.println("StatusText=Accepted");
out.println("paymentType=PaymentXY");
out = response.getWriter();
out.print(sw.toString());
out.flush();
out.close();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(DisplayHeader.class.getName()).log(Level.SEVERE, null, ex);
} }
Run Code Online (Sandbox Code Playgroud)
通过Firebug,这里发生了什么,我看到我已经收到510毫秒后生成的响应。我以为我会因为睡眠而需要1500毫秒以上的时间。我的理解是基于此信息:链接
我的WebAPI方法,它返回HttpResponseMessage与.csv文件内容:
private static HttpResponseMessage FileAsAttachment(string file)
{
var now = DateTime.Now;
var result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(file);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //attachment will force download
result.Content.Headers.ContentDisposition.FileName = string.Format("Report-{0}.csv", now.ToString("MMMM"));
return result;
}
Run Code Online (Sandbox Code Playgroud)
所以我只需点击功能,即可调用服务器:
$scope.GenerateReport = function() {
var endDate = '2016-04-30';
UserDaysSummary.generateReport({endDate: endDate }, function (result) {
console.log("Export");
});
}
Run Code Online (Sandbox Code Playgroud)
但是我所得到的只是内部数据的回应.我试图使用这个和这个答案把它作为文件,但这并没有改变任何东西.
优选地,对服务器的调用具有GET方法,顺便说一句
我有一个视图create_rating,在我提交表单后,我希望它在视图上处理rating_upload,然后我想重定向回到create_rating视图.似乎无法使用它,我的最新代码如下.我想当我点击它应发送到submit的create-rating页面时,我可以将其作为参数发回.文档也显示了这一点.我尝试了几件事最新的错误就是我所展示的......video_idrating_uploadcreate_rating
网址:
urlpatterns = [
url(r'^upload', UploadVideo.as_view(), name='upload'),
url(r'^(?P<pk>[0-9]+)/$', VideoView.as_view(), name='videoview'),
url(r'^(?P<video_id>\d+)/create_rating', create_rating, name='create_rating'),
url(r'^(?P<video_id>\d+)/rating_upload', rating_upload, name='rating_upload'),
url(r'^(?P<video_id>\d+)/rating_uploaded', rating_upload, name='rating_upload')
]
Run Code Online (Sandbox Code Playgroud)
观点:
def create_rating(request, video_id):
vid = get_object_or_404(Video, pk=video_id)
past_ratings = vid.rating.order_by('date_created')[:5]
template = loader.get_template('create_rating.html')
context = {
'vid': vid, 'past_ratings': past_ratings
}
return HttpResponse(template.render(context, request))
def rating_upload(request, video_id):
template = loader.get_template('rating_upload.html')
rated_video = Video.objects.get(pk=video_id)
context = {
'rated_video': rated_video
}
return HttpResponseRedirect(reverse('create_rating', video_id))
Run Code Online (Sandbox Code Playgroud)
模板,create_rating.html: …
httpresponse ×10
java ×4
django ×2
servlets ×2
android ×1
angularjs ×1
arrays ×1
asp.net ×1
bitmap ×1
c# ×1
django-views ×1
download ×1
http ×1
httpentity ×1
httprequest ×1
inputstream ×1
javascript ×1
json ×1
redirect ×1
reverse ×1
selenium ×1